有人可以解释一下实体框架中两者之间的区别。
例1:
obj = new TicketsEntities();
var depts = obj.DEPARTMENTs.Select( x => x);
string str = depts.GetType().ToString();
在这种情况下str print --- System.Data.Entity.Infrastructure.DbQuery`1 [LINQu.Models.DEPARTMENT]
例2:
obj = new TicketsEntities();
var depts = obj.DEPARTMENTs;
string str = depts.GetType().ToString();
在这种情况下str print --- System.Data.Entity.DbSet`1 [LINQu.Models.DEPARTMENT]
在任何一种情况下,当我们遍历depts时,我们得到相同的结果,那么两者之间有什么区别,哪一个更受欢迎?
答案 0 :(得分:1)
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.