编译的实体 SQL 查询和 OrderBy
我正在尝试使用 Entity SQL 提出已编译的查询,但在 ToList()
行上收到此错误:
LINQ to Entities 无法识别方法“System.Data. Objects.ObjectQuery`1[BLL.Company] OrderBy(System.String, System.Data.Objects.ObjectParameter[])' 方法,并且此方法无法转换为存储表达式。
这是我的代码尝试:
class param
{
public string where, orderby;
public int skip, take;
}
..
public class BLL.CompanyManager
{
private static readonly Func<DbContext, param, IQueryable<CompanyInfo>> companyList;
static MyClass()
{
companyList = CompiledQuery.Compile<DbContext, param, IQueryable<CompanyInfo>>(
(DbContext db, param p) =>
db.Companies.Include("Projects")
//.Where(p.where)
.OrderBy(p.orderby)
.Skip(p.skip)
.Take(p.take)
.Select(row => new CompanyInfo()
{
...
})
);
public static List<CompanyInfo> CompanyList(..)
{
...
List<CompanyInfo> list = new List<CompanyInfo>();
using (var db = (DbContext.Instance())
{
list.AddRange(companyList(db, params).ToList<CompanyInfo>()); // <== ERROR
}
return list;
}
}
}
I'm trying to come up with an compiled query using Entity SQL and I'm getting this error on ToList()
line:
LINQ to Entities does not recognize the method 'System.Data.Objects.ObjectQuery`1[BLL.Company] OrderBy(System.String, System.Data.Objects.ObjectParameter[])' method, and this method cannot be translated into a store expression.
Here's the code I'm trying:
class param
{
public string where, orderby;
public int skip, take;
}
..
public class BLL.CompanyManager
{
private static readonly Func<DbContext, param, IQueryable<CompanyInfo>> companyList;
static MyClass()
{
companyList = CompiledQuery.Compile<DbContext, param, IQueryable<CompanyInfo>>(
(DbContext db, param p) =>
db.Companies.Include("Projects")
//.Where(p.where)
.OrderBy(p.orderby)
.Skip(p.skip)
.Take(p.take)
.Select(row => new CompanyInfo()
{
...
})
);
public static List<CompanyInfo> CompanyList(..)
{
...
List<CompanyInfo> list = new List<CompanyInfo>();
using (var db = (DbContext.Instance())
{
list.AddRange(companyList(db, params).ToList<CompanyInfo>()); // <== ERROR
}
return list;
}
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
正如它所说,您不能在 L2E 查询中使用查询构建器方法。区别很微妙,因为它们看起来相同,但查询生成器和 L2E 不是同一件事。
Like it says, you can't use a query builder method in an L2E query. The distinction is subtle, because they look identical, but query builder and L2E aren't the same thing.