Nice programing

LINQ to Entities에서는 LINQ 식 노드 형식 'ArrayIndex'가 지원되지 않습니다.

nicepro 2020. 12. 29. 08:25
반응형

LINQ to Entities에서는 LINQ 식 노드 형식 'ArrayIndex'가 지원되지 않습니다.


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;
}

이 코드는 다음 예외를 발생시킵니다. The LINQ expression node type 'ArrayIndex' is not supported in LINQ to Entities.

그러나 x대신 공급 aa[i]하면 작동합니다.

왜?


이 문제를 해결하려면 임시 변수를 사용하십시오.

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

Where 절에서

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

람다 표현을 표현하는 방법입니다. 이것이 표현식으로 변환 된 다음 데이터베이스의 쿼리로 변환 aa[i]되면 배열에 대한 인덱스 인을 찾습니다 . 즉, 상수로 취급하지 않습니다 . 인덱서를 데이터베이스 언어로 번역하는 것은 불가능하기 때문에 오류가 발생합니다.


분명히 array index (aa[i])표현식 트리 내부 를 사용 하면 표현식으로도 변환하려고 시도합니다.

별도의 변수를 사용하여 문제를 해결하십시오.

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

 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];
    int temp = aa[i];
    Presentation press = context.Presentations.Where(m => m.PresId == temp).FirstOrDefault();
    paths.Add(press.FilePath);
}
return paths;
}

이 시도


SQL 유형 또는 함수에 매핑 할 수 없습니다.

목록과 배열을 서로 혼합하고 있음을 알고 있습니다. 이 코드에서 원하는 모든 작업은 목록을 사용하여 수행 할 수 있습니다.

다음 코드는 필요한 모든 작업을 수행합니다.

public List<string> GetpathsById(List<long> id) 
{ 
        long x; 
    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; 
} 

public List<string> GetpathsById(List<long> id) 
{ 
        long x; 
    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; 
} 

...에

public IEnumerable<String> GetpathsById(List<long> id) 
{ 
    foreach(long item in id) 
        yield return = (context.Presentations.Where(m => m.PresId == item).FirstOrDefault()).FilePath
} 

"짧은 스타일"이지만 다른 함수를 많이 작성하는 경우에는 권장하지 않습니다.


오류를 피하기 위해 단순화 할 수 있습니다.

public List<string> GetpathsById(List<long> id)
{
    return context.Presentations.Where(x => id.Contains(x.PresId)).Select(x => x.FilePath).ToList();
}

참조 URL : https://stackoverflow.com/questions/8353948/the-linq-expression-node-type-arrayindex-is-not-supported-in-linq-to-entities

반응형