linq-to-entities

LINQ to Entites: How should I handle System.InvalidOperationException when checking for existance of an item?

I have a many-to-one relationship that users can edit via checkboxes. PK of Foo is ID, and fid contains the id from the checkbox. I'm checking to see if an element exists with: Foo ent; try { ent = ctx.Foo.First(f => f.ID == fid); } catch (System.InvalidOperationException ioe) { ent = new Foo(); } It seems to me that I should be...

Entity LINQ on many-to-many got error "LINQ to Entities does not recognize the method 'Boolean Contains..."

I have 2 tables (Users and Roles) they are mapped as Many-to-Many in relational db. When I imported to Entity Data Content, they are still staying as the same relationship. Since they are mapped as Many-To-Many in Entity, I can access Users.RoleCollection or Roles.UserCollection However, when I execute this LINQ query, I got "LINQ...

Order by descending based on condition

Hello All, I want to write a LINQ to Entity query which does order by ascending or descending based on input parameter, Is there any way for that. Following is the my code. Please suggest. public List<Hosters_HostingProviderDetail> GetPendingApproval(SortOrder sortOrder) { List<Hosters_HostingProviderDetail> returnList ...

Order by descending is not working on LINQ to Entity

Order by descending is not working on LINQ to Entity In the following Query In place of ascending If I keep descending it is not working. Please help me out var hosters = from e in context.Hosters_HostingProviderDetail where e.ActiveStatusID == pendingStateId orderby e.HostingProviderName ascending select e; return host...

L2E many to many query

I have four tables: Users PrivilegeGroups rdPrivileges LinkPrivilege ----------- ---------------- --------------- --------------- userId(pk) privilegeGroupId(pk) privilegeId(pk) privilegeId(pk, fk) privilegeGroupId(fk) name code priv...

L2E delete exception

I have following code to delete an user from database: try { var user = from u in db.Users where u.Username == username select u; if (user.Count() > 0) { db.DeleteObject(user.First()); db.SaveChanges(...

L2E these two are the same thing?

Are the following two statements the same? Users user = db.Users.First(u => (u.Username == username)); and var user = from u in db.Users where u.Username == username select u; Users aUser = user.First(); ...

Update a record in L2S and L2E

I was told in L2S, the code for update and insert are the same, db.InsertOnSubmit(row); db.SubmitChanges(); and L2S will check to see if it is a insert or update and act approprately in the background. Is that true? How about L2E? I tested, looks like in L2E it is not like that. Maybe I did something wrong. ...

Left/Right/Inner joins using C# and LINQ

I am trying to figure out how to do a series of queries to get the updates, deletes and inserts segregated into their own calls. I have 2 tables, one in each of 2 databases. One is a Read Only feeds database and the other is the T-SQL R/W Production source. There are a few key columns in common between the two. What I am doing to set...

How to use LINQ To Entities for filtering when many methods are not supported?

Hi, I have a table in SQL database: ID Data Value 1 1 0.1 1 2 0.4 2 10 0.3 2 11 0.2 3 10 0.5 3 11 0.6 For each unique value in Data, I want to filter out the row with the largest ID. For example: In the table above, I want to filter out the third and fourth row because the fifth and sixth rows...

Creating Composite primary keys in a many-to-many relationship in Entity Framework

Hello, I have two tables that are connected via a join table in a many-to-many relationship in the Entity Framework. I need to add a composite primary key in the join table for the two columns that are related to the joined tables via standard foreign keys but I'm sure how to do that. ...

If-exists-UPDATE-else-INSERT with Linq-to-Entities?

is there a way to write this same SQL atomic instruction using Entities and LinQ? IF EXISTS (SELECT * FROM MyTable WHERE ID = @Id) UPDATE MyTable SET name = @name ELSE INSERT INTO MyTable (@Id, @name) or do you need to call a stored procedure from within the EF? ...

Problem with Contains

Hello there, I have a little problem which i can't solve. I want to use an SQL-In-Statement in Linq. I've read in this Forum and in other Forums that I have to use the .Contains (with reverse-thinking-notation :-)). As input i have a List of Guids. I first copied them into an array and then did something like that : datatoget = (from p...

How can I use table data type in LINQ to Entity

Hello guys, I have a requirement of updating set of N records into the db table at a time. How can I achieve it in LINQ to Entity(.edmx file). If I update them using conventional procedure in LINQ To Entiy, It wont be good practice, How can I achieve this. ...

Does Linq to SQL or Linq to Entities 4.0 support the hierarchyid datatype

Is their a way to use linq to SQL/Entities 4.0 to work with the hierarchy datatype? ...

LINQ to SQL vs Entity Framework for an app with a future SQL Azure version

I've got a vertical market Dot Net Framework 1.1 C#/WinForms/SQL Server 2000 application. Currently it uses ADO.Net and Microsoft's SQLHelper for CRUD operations. I've successfully converted it to Dot Net Framework 4 C#/WinForms/ SQL Server 2008. What I'd like to do is also offer my customers the OPTION to use SQL Azure as a backen...

T-SQL Where statement - OR

Sorry for the vague title but I really didn't know what title to give to this problem: I have the following result set: ID Field FieldValue Culture 1 Color Groen nl-NL 2 Taste Kip nl-NL 3 Color Green en-GB 4 Taste Chicken en-GB 5 Color Green en 6 Taste Chicken ...

LINQ to Entites: Doing a count over one-to-many relationships

I have a couple of tables where there are one to many relationships. Let's say I have a Country table, a State table with a FK to Country, and a City table with a FK to State. I'd like to be able to create a count of all Cities for a given country, and also a count of cities based on a filter - something like: foreach( var country in ...

Why does LINQ-to-Entites recognize my custom method?

This works: (from o in Entities.WorkOrderSet select o) .Where(MyCustomMethod); This does not: (from o in Entities.WorkOrderSet select o) .Where(o => MyCustomMethod(o)); ([Edit] Even without new, it doesn't work) I understand why the second doesn't work - but why in the world does the first work!? Shouldn't I get a "LINQ-to-Enti...

Method 'SingleOrDefault' not supported by Linq to Entities

I read other posts on similar problem on using SingleOfDefault on Linq-To-Entity, some suggested using "First()" and some others suggested using "Extension" method to implement the Single(). This code throws exception: Movie movie = (from a in movies where a.MovieID == '12345' select a).SingleOrDefaul...