linq-to-entities

Entity Framework vs Linq to Entities vs Linq to SQL

Hi I read a lot articles about how to work with database in WPF .Net 4 application. As I understood, the main two technologies are: Linq to SQL (L2S) Entity Framework (EF) but sometimes I also can see mention of Linq to Entities (L2E) technology, but can't find clear explanation what difference between EF and L2E. So, my question i...

Dynamically creating an Expression calling method EntityFunctions.DiffDays

I am trying to create the following Where clause expression dynamically: context.Cars. Where(c => EntityFunctions.DiffDays(c.Created, c.Created) == null). ToList() This is the code I am using to create the expression: var parameter = Expression.Parameter(typeof(Car), "c"); var property = Expression.Property(parameter, "Created"); var...

Related data in Entity Framework projection query not being tied together

My Database contains three tables: PermissionCategories, Permissions, and Users. There is a many to many relationship between Permissions and Users which is resolved by a UserPermissions table. Using Entity Framework projection I'm trying to get all the PermissionCategories and include (eager load) the permissions filtered by userId. M...

Linq2EF: Update Records with result of a CheckBoxList / Set of ID Values

Say I have a CheckBoxList on a page reflecting table data. A user unchecks previously checked items, and checks ones that were not checked. I want to update the database with LINQ2EF so that the records remaining match the newly submitted checked items. In other words, when the page submits, I get a String[] of checked IDs. I then need ...

How to write linq to join two tables with no relationship?

Suppose I have two tables: Tab1(id, shareid, ....) Tab2(id, shareid, ...) DB was modeled by EF. Then I want to a linq get same result as following sql: select t1.* from Tab1 t1 join Tab2 t2 on t1.shareid=t2.shareID So linq should be somthing like: ObjectContext.Tab1s.Where(...); How to write the linq for this request? ...

Arithmetic operations on subqueries using Expression.Subtract(?)

I'm trying to create an expression tree that's similar to performing subqueries like: SELECT (SELECT Sum(Foo) FROM Bar1) - (SELECT Sum(Foo) FROM Bar2)) I'm trying to reuse 2 expression trees that are too complex to repeat. What I have right now is 2 (simplified) expression trees: Expression<Func<Bar, int>> SumBar1 = (bar) => (fr...

How to get an outerjoin on a Linq query

Given this linq query from c in context.Customers from o in c.Orders where c.City == "MyCity" || o.ShipTo == "MyCity" select c the query will not return any rows if the customer's city is "MyCity" but does not have any orders. This is because of the implied inner join between Customers and Orders. How do I select customers with a Ci...

LIKE with Linq to Entities

I know the .Contains() method does like LIKE %therm%, the .StartsWith() method does like LIKE therm% and the .EndsWith() method like LIKE %therm but... Is there a way to do like below on **Linq to Entities**? SELECT * FROM [dbo].[Users] WHERE Name LIKE 'rodrigo%otavio%diniz%waltenberg' PS: I'M USING LINQ TO ENTITIES. NOT LINQ TO SQL ...

Entity Framework NoTracking not working

I'm trying to run a no tracking query on my entities so that I can update them outside of the context. However, when the no tracking is not working and I get an exception stating "The ObjectContext instance has been disposed and can no longer be used for operations that require a connection." This exception is thrown by a property wh...

"Or" Together Expression<Func<EntityFramework Entity, bool>> in .NET 4, Silverlight RIA domain service

Hi, I have a small custom object defined as: public class TimeSeriesDefinition { public int classID; public DateTime startTime; public DateTime endTime; } I'm passing a List classIDs, a List startTimes, and a List endTimes into an RIA Domain Service function. As a matter of organization, I was grouping these values into...

Adding two entities with FKs pointing to each other

I have two tables in my application. The first one, Sections, stores content for a specific section of an online publication. This contains (among other things) the section title and a field CurrentRevisionId (FK). A second table, Revisions, stores different revisions of that content. It has PK RevisionId field (the FK above, used to...

LINQ-to-Entitites: How to do effective lazy-loading (not 1+N selects)?

Short version: Starting with a List of entities and needing all dependent entities through an association, is there a way to use the corresponding navigation-propertiy to load all child-entities with one db-round-trip? Ie. generate a single WHERE fkId IN (...) statement via navigation property? Long version: I am generating a report fro...

Object Must Implement IConvertible LINQ to Entities query containing grouping

Hi. I have an ASP.NET MVC application in which i am using entity model for connecting to MySQl database Here I am joining 2 tables and then applying group by on one of the fields. Then fetching the result from GroupBy result. Everything is working fine on development machine but getting next error: Object Must Implement IConvertible...

DomainService Include Get Child/Sub Entities in the client?

How do I include child entities from a DomainService, without the chance of my code being overwritten when I generate a new DomainDervice? I am trying to include sub entities with my query. For instance, I have a ParentTable and a ChildTable. I can retrieve the ChildTable entities as follows: Modify the DomainService metadata file to...

can I sort a deferred loading collection property on a wcf dataservice entity

I have a wcf dataservice I am calling from Silverlight and I am expanding a collection property on an entity and I want to be able to sort the items in the expanded property by specifying it in the query. Is there a way to do this? here is the expand linq : - I want the Videos collection to be sorted by a property called SortOrder on t...

Entity Framework 4: How to code projection to a class type?

If I have a class like the following: public class Customer { public int id {get;set;} public string name {get;set;} public string line1 {get;set;} public string line2 {get;set;} public string line3 {get;set;} public string line4 {get;set;} } And I only want to select the ID and Name values, leaving the ...

Insert Data into mulitple table using FK's using Entity framework

Hi I have tables Movie - ID, Name, Description, DirectorID-Fk, MusicID-Fk Director - ID, Name Music - ID, MusicName Now I am trying to insert a new record to movie but I dont know whether Director is already existing in the DB and same with the Music). From UI i am taking all movie information, DirectorName and MusicName. Now wh...

Entity Framework Simple Report

How would I write a query using entity framework where I need to make a list with a column for the product, category and parent category. I have not figured out how to get the parent category. Any Help is appreciated. So far I have the following: from product in Products select new { Ctg = (from prdCategory in ProductCategories ...

Entity Framework 4 Abstract Model - How to Programatically Eager-Load Navigational Properties?

Hi Guys, I have an EF4 Model that is built with abstract entities/classes: Notice how State entity has a navigational property called Country. Note: I have lazy-loading disabled, so i must eager-load on demand. Now, if i have the following method: public Location FindSingle(int id) { return _repository.Find().WithId(id).SingleO...

Optimizing multiple LINQ to Entity Framework queries

Hello, I have 2 Entity Framework entity sets with many-to-many relationship (compromised of 3 DB tables). For the sake of the example let's say I have a Worker entity and a Company entity, where each worker can work at multiple companies and each company can have many workers. I want to retrieve for each worker all the companies that h...