Reputation: 15104
Can someone please explain whats the difference between the two in Entity Framework.
Example1:
obj = new TicketsEntities();
var depts = obj.DEPARTMENTs.Select( x => x);
string str = depts.GetType().ToString();
In this case str prints --- System.Data.Entity.Infrastructure.DbQuery`1[LINQu.Models.DEPARTMENT]
Example2:
obj = new TicketsEntities();
var depts = obj.DEPARTMENTs;
string str = depts.GetType().ToString();
In this case str prints --- System.Data.Entity.DbSet`1[LINQu.Models.DEPARTMENT]
In either case when we loop through the depts we get same result , so what is the difference between the two , and which one is preferred ?
Upvotes: 7
Views: 4709
Reputation: 781
The DbSet represents the set of data and can manipulate the data by exposing methods like Add, Update, Remove. The DbQuery represents a Linq query that is executed on a set of data. It does not have the Add, Update and Remove methods.
In your case I think there is no real difference, but for simplicity sake I would pick your second example since the Select(x=>x) is not neccessary.
Upvotes: 3