LINQ To Entities 无法识别数组索引

发布于 2024-12-19 04:37:32 字数 1197 浏览 3 评论 0原文

我的代码中有以下函数

  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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

时光无声 2024-12-26 04:37:32

这并不神奇:表达式树被转换为关系数据库可以理解的 SQL 查询。您几乎可以在表达式树中执行任何操作。不幸的是,并非所有操作都已实施。考虑以下示例:

Presentation press = context
   .Presentations
   .Where(m => SomeCustomFunctionThatUsesUnmanagedPInvokeCode(m.PresId))
   .FirstOrDefault();

您期望生成的 SQL 查询是什么?

数组索引器就是这种情况。它们无法转换为 SQL 查询。

话虽这么说,在您的情况下,以下内容可能会更简单一些:

public List<string> GetpathsById(List<long> id)
{
    return
        (from p in context.Presentations
         where id.Contains(p.PresId)
         select p.FilePath
        ).ToList();
}

.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:

Presentation press = context
   .Presentations
   .Where(m => SomeCustomFunctionThatUsesUnmanagedPInvokeCode(m.PresId))
   .FirstOrDefault();

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:

public List<string> GetpathsById(List<long> id)
{
    return
        (from p in context.Presentations
         where id.Contains(p.PresId)
         select p.FilePath
        ).ToList();
}

The .Contains method will be translated into a SQL IN clause. This avoids sending multiple SQL queries to the database as you do in your example on each iteration.

彩虹直至黑白 2024-12-26 04:37:32

这个问题是由另一个用户提出的,所以它一定是一个学校作业。

基本上我给了其他用户同样的答案。

它无法映射到 SQL 类型或函数。

您想要在此代码中执行的所有操作都可以简单地使用列表并以稍微不同的方式迭代它来完成。

下面的代码将完成您需要的一切。

public List<string> GetpathsById(List<long> id)  
{
    List<string> paths = new List<string>();  
    foreach(long aa in id)  
    {  
        Presentation press = context.Presentations.Where(m => m.PresId == aa).FirstOrDefault();  
        paths.Add(press.FilePath);  
    }  
    return paths;  
}  

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.

public List<string> GetpathsById(List<long> id)  
{
    List<string> paths = new List<string>();  
    foreach(long aa in id)  
    {  
        Presentation press = context.Presentations.Where(m => m.PresId == aa).FirstOrDefault();  
        paths.Add(press.FilePath);  
    }  
    return paths;  
}  
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文