【发布时间】:2015-07-17 11:33:07
【问题描述】:
我有以下(工作)代码:
List<Location> allLocations = new List<Location>();
using (MyContext context = new MyContext())
{
Login currentLogin = GetCurrentLogin(context);
if (currentLogin != null)
{
foreach (var customer in currentLogin.Customers)
{
allLocations.AddRange(customer.Locations);
}
}
}
return allLocations.AsQueryable();
MyContext 及其对象存在于实体框架中。 Customers 和 Locations 是 ICollection<>-属性
此代码按预期工作,返回用户客户的所有位置
但如您所见,我将实体 customer.Locations 添加到 List。
在该函数结束时,我将生成的列表返回为IQueryAble,以便能够继续在结果上使用LinQ-Expressions。
由于性能原因,我想跳过列表-步骤并留在IQueryAble
有可能吗?
【问题讨论】:
-
你为什么要返回
IQueryable<T>?您可以在List<T>上使用 LINQ 表达式就好了。当然,它们都将在内存列表中工作,而不是进入数据库,但你当前的代码也不会有任何不同。 -
这就是我不喜欢我的代码的原因,这就是我所说的“性能原因”
标签: c# linq entity-framework iqueryable