LINQ To Entities 无法识别数组索引
我的代码中有以下函数
public List<string> GetpathsById(List<long> id)
{
List<string> paths = new List<string>();
for (int i = 0; i < id.Count; i++)
{
Presentation press = context.Presentations.Where(m => m.PresId == id[i]).FirstOrDefault();
paths.Add(press.FilePath);
}
return paths;
}
但是当我尝试这个时,编译器会出现这样的错误。
LINQ to Entities does not recognize the method 'Int64 get_Item(Int32)' method, and this method cannot be translated into a store expression.
然后我尝试做这样的事情,一切正常。
public List<string> GetpathsById(List<long> id)
{
long x;
List<string> paths = new List<string>();
for (int i = 0; i < id.Count; i++)
{
x = id[i];
Presentation press = context.Presentations.Where(m => m.PresId == x).FirstOrDefault();
paths.Add(press.FilePath);
}
return paths;
}
所以我想知道,为什么?对于这种行为,我在脑海中找不到任何答案。谁能解释这个悖论吗?
I have following function in my code
public List<string> GetpathsById(List<long> id)
{
List<string> paths = new List<string>();
for (int i = 0; i < id.Count; i++)
{
Presentation press = context.Presentations.Where(m => m.PresId == id[i]).FirstOrDefault();
paths.Add(press.FilePath);
}
return paths;
}
But when I try this, compiller get error like this.
LINQ to Entities does not recognize the method 'Int64 get_Item(Int32)' method, and this method cannot be translated into a store expression.
Then I try to do something like this and all works fine.
public List<string> GetpathsById(List<long> id)
{
long x;
List<string> paths = new List<string>();
for (int i = 0; i < id.Count; i++)
{
x = id[i];
Presentation press = context.Presentations.Where(m => m.PresId == x).FirstOrDefault();
paths.Add(press.FilePath);
}
return paths;
}
So I wonder, why? I can't get any answer for that behaviour in my mind. Can anyone explain this paradox?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
这并不神奇:表达式树被转换为关系数据库可以理解的 SQL 查询。您几乎可以在表达式树中执行任何操作。不幸的是,并非所有操作都已实施。考虑以下示例:
您期望生成的 SQL 查询是什么?
数组索引器就是这种情况。它们无法转换为 SQL 查询。
话虽这么说,在您的情况下,以下内容可能会更简单一些:
.Contains
方法将被转换为SQL IN
子句。这可以避免像您在每次迭代的示例中所做的那样向数据库发送多个 SQL 查询。There's no magic: expression trees are translated into SQL queries which is what relational databases understand. You could do almost anything in an expression tree. Unfortunately not all operations are implemented. Consider the following example:
What do you expect the generated SQL query to be?
That's the case with array indexers. They cannot be translated into SQL queries.
This being said, in your case, the following might be a little simpler:
The
.Contains
method will be translated into aSQL IN
clause. This avoids sending multiple SQL queries to the database as you do in your example on each iteration.这个问题是由另一个用户提出的,所以它一定是一个学校作业。
基本上我给了其他用户同样的答案。
它无法映射到 SQL 类型或函数。
您想要在此代码中执行的所有操作都可以简单地使用列表并以稍微不同的方式迭代它来完成。
下面的代码将完成您需要的一切。
This question was asked by another user so it must be a school assignment.
Basically I gave this same answer to the other user.
It cannot be mapped to an SQL type or function.
Everything you want doing in this code can be done simply using the list and iterating over it in a slightly different way.
The following bit of code will do everything you need it to.