在执行以下 Linq to Sql 语句时出现“代码应该无法访问”错误。我正在使用 EF 6.1.3。我认为这是一个与过滤导航属性相关的已知错误。似乎它可能已在 EF7 中修复,但我在 EF 6.2 发行说明和 GitHub 上的 EF6 开放项目中都没有看到与此相关的任何内容,所以我想我正在寻找解决方法,也许是一种不同的写作方式我的 Linq 声明。
最终,我在多个地方应用相同的 where 子句 (AccountSecurity)(也是基于传递用户 key 参数)所以我想我可以将它变成一个函数,生成表达式以在我的 Linq to Sql 语句中的 where 子句。
public List<Company> GetCompanyList(int p_UserKey, MemberType p_MemberType)
{
var qry = from cs in this.DbContext.CompanySet
.Where(c => c.Accounts.AsQueryable().Any(this.AccountSecurity(p_UserKey)))
select cs;
return qry.ToList();
}
private Expression<Func<Account, bool>> AccountSecurity(int p_UserKey)
{
return (ac => ac.UserAccounts.Any(ua => ua.UserKey == p_UserKey));
}
最佳答案
作为解决方法,您可以将方法调用捕获到查询表达式树外部的变量中,并在内部使用该变量:
var accountFilter = this.AccountSecurity(p_UserKey);
var qry = from cs in this.DbContext.CompanySet
.Where(c => c.Accounts.AsQueryable().Any(accountFilter))
select cs;
return qry.ToList();
关于c# - 系统核心错误 : "Code supposed to be unreachable" using C# Entity Framework and Linq with Expression,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47062098/