谁能告诉我以下内容有什么问题以及如何解决它:
//filterItem.Value is a string array - It is being conerted into an int array
var intFilters = Array.ConvertAll(filterItem.Value, s => int.Parse(s));
//Returns an IQueryable<int> for all items in this context that are also in intFilters
var ids = Context.table1.Where(a => intFilters.Any(y => y == a.domId)).Select(x => x.domId);
//query is an IQueryable
query = query.Where(x => specUnidsNullable.Contains(y => y == x.Id));
Run Code Online (Sandbox Code Playgroud)
query上述的目的是仅获取也包含在中的记录ids
我收到以下错误:
Cannot convert lambda expression to type int because it is not a delegate type
Run Code Online (Sandbox Code Playgroud)
我也尝试过这个:
var ids = idsNullable.Where(x => x > 0);
Run Code Online (Sandbox Code Playgroud)
效果很好 - 我这样做的原因是看看问题是否在于ids无法转换为 lambda 类型表达式。
关于此错误有很多问题,但似乎没有一个问题能解决我所看到的特定问题......
specUnidsNullable.Contains(y => y == x.Id)调用 LINQContains方法,该方法需要给定类型的单个值(int我想在本例中),并且您在那里传递表达式。所以更改为
specUnidsNullable.Contains(x.Id);
Run Code Online (Sandbox Code Playgroud)
或者
specUnidsNullable.Any(y => y == x.Id);
Run Code Online (Sandbox Code Playgroud)