Art*_*yan 56 c# linq arrays linq-to-entities entity-framework
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;
}
Run Code Online (Sandbox Code Playgroud)
此代码抛出以下异常: The LINQ expression node type 'ArrayIndex' is not supported in LINQ to Entities.
但是,如果我提供x
而不是aa[i]
它的工作.
为什么?
Geo*_*ett 89
要修复此问题,请使用临时变量:
var tmp = aa[i];
...
m => m.PresId == tmp
Run Code Online (Sandbox Code Playgroud)
在你的where子句中
m => m.PresId == aa[i]
Run Code Online (Sandbox Code Playgroud)
这是表达lambda表达式的一种方式.当它转换为表达式,然后转换为数据库的查询,它找到aa[i]
,这是一个数组的索引.即它不会将其视为常数.由于索引器转换为数据库语言是不可能的,因此会出错.
Tra*_*x72 14
显然,如果你array index (aa[i])
在表达式树中使用它,它也会尝试将其转换为表达式.
只需使用单独的变量解决它:
int presId = aa[i];
Presentation press = context.Presentations.Where(m => m.PresId == presId).FirstOrDefault();
Run Code Online (Sandbox Code Playgroud)
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;
}
Run Code Online (Sandbox Code Playgroud)
尝试这个
归档时间: |
|
查看次数: |
32886 次 |
最近记录: |