LINQ to Entities无法识别方法Int32 get_Item(Int32)

Mur*_*zel 59 .net linq entity-framework

我是关于实体框架和linq的新手.我的查询是这样的

var query = (from d in db.MYTABLE
             where d.RELID.Equals(myInts[0])
             select d.ID).Distinct();

List<int?> urunidleri = query.ToList();
Run Code Online (Sandbox Code Playgroud)

当我执行此代码时,我收到错误消息"LINQ to Entities无法识别方法Int32 get_Item(Int32)".我怎样才能解决我的问题?

谢谢...

Joh*_*zen 135

您需要将您存储int在变量中,以便EntityFramework不会尝试将整个数组拉入其范围.

var myInt = myInts[0];

var query = (from d in db.MYTABLE
             where d.RELID.Equals(myInt)
             select d.ID).Distinct();

List<int?> urunidleri = query.ToList();
Run Code Online (Sandbox Code Playgroud)

  • 因为C#正在生成涉及数组的表达式树.这些表达式由EF转换为SQL,EF不知道如何处理该数组. (6认同)
  • 一般来说,为什么LINQ to Entities试图在那里调用get_Item(Int32)?(问题发帖) (3认同)

Alb*_*nbo 7

var firstInt = myInts[0];
var query = (from d in db.MYTABLE
             where d.RELID.Equals(firstInt)
             select d.ID).Distinct();

List<int?> urunidleri = query.ToList();
Run Code Online (Sandbox Code Playgroud)