LINQ To Entities doesn't recognize array index

后端 未结 2 1552
难免孤独
难免孤独 2021-01-25 00:02

I have following function in my code

  public List GetpathsById(List id)
        {

            List paths = new List<         


        
2条回答
  •  既然无缘
    2021-01-25 00:44

    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 GetpathsById(List 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.

提交回复
热议问题