linq-to-sql

Linq To SQL Insert Object with Child List

Hello I have the following object [Table("Order")] public class Order { [Column(IsPrimaryKey=true)] public int id; public List<OrderItem> _OrderItems; } public class OrderItem { public int id; public double TotalValue; } The OrderItem should be mapped, but not sure how yet - unless I should do it the same and it will just wo...

How can I see the types by which an InvalidCastException is raised in C#?

In my LINQ-to-SQL DAL, I'm getting a "Specified Cast Was Not Valid." Here is the exact error: System.InvalidCastException was unhandled by user code Message="Specified cast is not valid." Source="System.Data.Linq" StackTrace: at System.Data.Linq.IdentityManager.StandardIdentityManager.SingleKeyManager`2.TryCreateKeyFromValues(Ob...

C# LINQ-TO-SQL OR-Statement in JOIN

How can I make the following SQL a Linq query: SELECT * FROM ORDERS INNER JOIN ITEMS ON ORDERS.ID = ITEMS.ORDER_A OR ORDERS.ID = ITEMS.ORDER_B I would think it would be: from o in orders join i in items on o.ID equals i.OrderA or o.ID equals i.OrderB select new { Order = o, Item = i } I'm guessing the compiler wants something else....

Comparison operators not supported for type 'System.Linq.IQueryable`1[System.Int32]'

Hi Geeks, I met a issue with LTS My code as below: return from p in _db.Companies where p.Deleted == false && (from q in _db.Contacts_Companies where q.ContactId == contactId && q.Deleted == false select q.CompanyId).Equals(p.Id) select p; I know the issue comes from the CompanyId.Equals(Id) they are both Int32, but I am a bit confus...

Linq to SQL - Filter by "All Children" in "FilterCollection"

I want to return all the entities of a database where ALL child elements of a parent are found in the control collection. So, I thought something like this would work: var toInclude = new List<int>{1, 2, 3}; var result = allObjs.Where(obj => obj.Children.All(child => toInclude.Contains(child))); That doesn't work. It returns all ent...

Is there a point at which an Enum can get too bloated?

I've defined an Enum as part of the model objects for an ASP.NET MVC application. The Enum is called 'ContentTypes' and looks something like this: public enum ContentTypes { [Description("News story")] NewsStory = 1, [Description("Article")] Article = 2 } Now I'm planning to add another set of attributes to the enum ...

Can't get Linq To Sql log output

Hello L2S is throwing me back an exception from my database when I try to run a command. Unfortunately I can't see the SQL that is running no matter what I do. I set the DataContext.Log to Console.Out -> nothing in my Debug window, or even when I run DebugView. I try to get it to write to a file - nothing. I try SQL Profiler - appar...

What would you do to avoid conflicting data in this database schema?

Hi, I'm working on a multi-user internet database-driven website with SQL Server 2008 / LinqToSQL / custom-made repositories as the DAL. I have run across a normalization problem which can lead to an inconsistent database state if exploited correctly and I am wondering how to deal with the problem. The problem: Several different compa...

linq2SQL + OrderBy string value

Hi Guys I wonder if this is possible var table = _db.GetTable<T>(); var data = table.Where(t => !t.Deleted).OrderBy("Name"); I cannot do t.Name as t only have Id and Deleted The base class which contains this method looks like this public class RepositoryBase<T> where T : class, Interfaces.IModel IModel only knows of Deleted an...

Error occurs when checking existence of records by linq to sql

I want to check whether records exist under conditions. This line works fine: DataContext.Alerts.Count(e => e.MemberShip.email == email) > 0 This line gives error like "Cannot convert lambda expression to type because it is not a delegate type" DataContext.Alerts.Contains(e => e.MemberShip.email == email) How can I modify the sec...

Is it safe to convert varchar and char into nvarchar and nchar in SQL Server?

We currently have a number of columns in the database which are of type varchar. The application that uses them are in C# and it uses Linq2Sql for the communication (or what to call it). We would like to support unicode characters, which means we would have to convert the varchar columns into nvarchar. Is this a safe operation? Is it j...

Why am I getting "Cannot insert explicit value for identity column" in LINQ to SQL when I'm not specifying a value for an identity column?

I have a table that looks like this: ClientID is the only identity column I have in the table. UserID is a FK to a different tables primary key. Here is my Linq to SQL insert code: public void InsertClientByUsername(string username, Entities.Client clientInfo) { using (LinqModelDataContext db = new LinqModelDataContext()) {...

help with simple linq query

My table has the following columns: User (UserID, GroupID, userName, userType, ...) I need to do this: SELECT GroupID FROM Users WHERE userID = @userID AND username = @username and usertype = @usertype. I have my DataContext ready to go, guidance on my LINQ query would be great! ...

LINQ to SQL Many to Many Comparison Query

I have just finished creating a custom role provider using LINQ to SQL for data access. One of the functions you need to write for the provider is to remove users from roles; public void RemoveUsersFromRoles(string[] usernames, string[] rolenames); Now one approach would be; return a list of roles iterate for each role and remove t...

LINQ to SQL Classes not CLS-Compliant?

I'm using LINQ to SQL as my data access layer for a new project. I have added my database tables to the designer and all is well. Whenever I use one of these classes in a function, Visual Studio warns me that 'Type xxxx is not CLS-compliant' or that 'Return type of function xxxx is not CLS-compliant' Is this a problem with the classes...

view sql that linq-to-sql produces

Hi, Other than using profiler, is there a way to view the sql that linq-to-sql produces? ...

Store procedure select all fields from One table using join

Hello I am very frustrated from linq to sql when dealing with many to many relationship with the skip extension. It doesn't allow me to use joinned queries. Not sure it is the case for SQL server 2005 but I am currently using SQL Server 2000. Now I consider to write a store procedure to fetch a table that is matched by two tables e.g...

asp.net mvc 2 preview - Templated Helpers linq to sql

I'm checking out these neat Templated Helpers that have juts been released with the latest preview of ASP.NET MVC. I notice that I can decorate the classes as required, but I'm thinking it will get a bit messy with Linq 2 Sql if I have to keep re-generating the classes when I make schema changes. Does any one have a recommended approac...

how to loop inside every tables/columns in a dbml file to create new partial class with more information about every columns.

I have a class that add extra information about a column for linq2sql (see code below) right now, I have to explicitly tell what column I want that info on, how would you put that code with a loop on every column in every table from a dbml file? I was doing my test on a very very small DB, now I have to implement it on a much more bigg...

Linq to Sql Force Order By

Is there a way in Linq to Sql (.net 3.5) to specify that when you get the children of a particular record, to force the list of children to come back in a specific order? For example, I have a list that has a "DisplayOrder" on it. When I ask my parent record/object for it's property that returns to me this list, I want Linq to Sql to r...