我正在尝试使用谓词构建器通过MVC3执行查询。我收到了错误
“无法将类型'System.Boolean'强制转换为'System.Object'.LINQ to Entities仅支持转换实体数据模型基元类型。”
我的查询传递如下
if (!string.IsNullOrEmpty(SearchObj.EmployeeIDSortId.ToString()) && SearchObj.EmployeeIDSortId > 0)
{
string[] vals =new string[' '];
if (SearchObj.EmployeeIDSortId == 1)
{
vals = new string[] { "a", "b", "c", "d", "e", "f", "g", "h", "i", "j", "k","l" };
}
else if (SearchObj.EmployeeIDSortId == 2)
{
vals = new string[] { "m", "n", "o", "p", "q", "r", "s" };
}
else if (SearchObj.EmployeeIDSortId == 3)
{
vals = new string[] { "t", "u", "v", "w", "x", "y", "z" };
}
employeepredicate =
employeepredicate.And(emp => emp.EmployeeName.Substring(0, 1).ToLower().Equals(vals.Any()));
}
你能告诉我这有什么问题,我想这只是因为我的查询vals.Any()。
谢谢
答案 0 :(得分:2)
我先写一下 - 我不熟悉And
方法,但.Equals(vals.Any())
看起来不对我。所以,我假设And
接受一个返回bool的函数。您真正想要的是找出员工姓名中的第一个字符是否包含在vals数组中:
employeepredicate = employeepredicate.And(
emp => vals.Contains(emp.EmployeeName.Substring(0, 1).ToLower()));