The ArrayIndex LINQ expression node type is not supported in LINQ to Entities.

arrays c# entity-framework linq linq-to-entities

Question

public List<string> GetpathsById(List<long> id)
{
    long[] aa = id.ToArray();
        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 == aa[i]).FirstOrDefault();
        paths.Add(press.FilePath);
    }
    return paths;
}

These exceptions are thrown by this code:The LINQ expression node type 'ArrayIndex' is not supported in LINQ to Entities.

Though if I providex in place ofaa[i] It operates.

Why?

1
57
3/8/2018 12:32:18 PM

Accepted Answer

Use a temporary variable to remedy this:

var tmp = aa[i];
...
m => m.PresId == tmp

Your where clause includes

m => m.PresId == aa[i]

in which a lambda expression is expressed. It detects the information when that is transformed into an expression and then into a query on your database.aa[i] which represents an array's index. It does not, however, consider it as a constant.. Since it is difficult to translate an indexer into your database language, an error results.

91
1/12/2016 11:45:09 AM

Popular Answer

Evidently, if you use anarray index (aa[i]) It attempts to make it into an expression inside of an expression tree.

Just use a different variable as a workaround:

int presId = aa[i];
Presentation press = context.Presentations.Where(m => m.PresId == presId).FirstOrDefault();


Related Questions





Related

Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow
Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow