我有一个非常简单的Linq查询,显示运行时错误
查询为
// string[] Blocks = _TableView.permission.BlockList.Split(',');
// string[] Permissions = _TableView.permission.PermissionsList.Split(',');
string[] Blocks = ... some string array ...;
string[] Permissions = ... some string array ... ;
var test = await (from t in _context.DocumentIn
where
Permissions.Contains(t.Imp_Exp.ToString()) &&
Permissions.Contains(t.confLevel.ToString()) &&
Blocks.Contains(t.BlockName)
select t).ToListAsync();
错误是
could not be translated. Either rewrite the query in a form that can be translated,
or switch to client evaluation explicitly by inserting a call to either AsEnumerable(),
AsAsyncEnumerable(), ToList(), or ToListAsync().
一些笔记
跟踪时,我发现块和权限充满了值。
当我从查询中删除两个权限条件时,它的工作原理如下所示:
var test = await (from t in _context.DocumentIn
where
/*Permissions.Contains(t.Imp_Exp.ToString()) &&
Permissions.Contains(t.confLevel.ToString()) &&*/
Blocks.Contains(t.BlockName)
select t).ToListAsync();
答案 0 :(得分:1)
如果Permissions
是基于整数的字符串数组,一个简单的解决方案是将该数组转换为int
:
int[] Permissions = _TableView.permission
.PermissionsList
.Split(',')
.Select(s => int.Parse(s)).ToArray();
并删除引起异常的ToString
调用:
var test = await (from t in _context.DocumentIn
where Permissions.Contains(t.Imp_Exp) &&
Permissions.Contains(t.confLevel) &&
Blocks.Contains(t.BlockName)
select t).ToListAsync();
但是主要思想是,无论您遇到什么情况,都删除ToString
调用,您应该尝试将其从查询中删除