我有以下2种EF型号:如何这个LINQ转换为实体查询到“方法语法”而不是“查询语法”
public class Rule
{
public int Id { get; set; }
public string RuleValue { get; set; }
public bool IsActive { get; set; }
public List<Exclusion> Exclusions { get; set; }
}
public class Exclusion
{
public int Id { get; set; }
public int ApplicationId { get; set; }
public int SiteId { get; set; }
public int RuleId { get; set; }
[ForeignKey("RuleId")]
public Rule Rule { get; set; }
}
我想查询数据库返回一个列表,但只有在Exclusions表中没有相关记录,基于相关的RuleId以及指定的ApplicationId和SiteId。最终,考虑到任何应用程序/网站特定的排除事项,以便在我返回的结果中不包括这些规则。
我已经能够做到这一点至今使用以下查询:
IQueryable<Rule> query =
from r in context.Rule
where r.IsActive && !(from e in context.Exclusion
where e.ApplicationId == applicationId &&
e.SiteId == siteId
select e.RuleId)
.Contains(r.Id)
select r;
我总是使用方法语法其他地方和一致性,不希望有使用查询语法仅这一个方法,但我无法使用Method Syntax获得相同的功能。
我按照你的代码尝试了这种方法,但是它返回的结果不正确,但是当我改变它时,那里不是两个.Where()子句,而是使用了.Where(r => r.IsActive &&!r.Exclusions .Any(等))然后我开始把结果返回到我的预期。很好,谢谢!我确信在使用查询语法之前我已经尝试了这种方法,但是我一定会弄错它的!再次感谢。 – marcusstarnes
。其中(x).Where(y)等于.Where(x && y)。你应该得到相同的结果 –