C# 在另一个Linq查询的Where子句中使用Linq Join
我想有Linq查询如下,但不能,它给我编译时错误请任何帮助?错误是“推理无法调用联接”-请提供帮助?我想在其他查询的where子句中使用join,这可能吗C# 在另一个Linq查询的Where子句中使用Linq Join,c#,linq,C#,Linq,我想有Linq查询如下,但不能,它给我编译时错误请任何帮助?错误是“推理无法调用联接”-请提供帮助?我想在其他查询的where子句中使用join,这可能吗 Case _case = (from x in this.Context().CaseRepository.GetAll() where (from g in x.Violations join a in this.Context().OneToManyRepository
Case _case = (from x in this.Context().CaseRepository.GetAll()
where (from g in x.Violations
join a in this.Context().OneToManyRepository.GetAll().Where(a => a.ParentEntity == "Notice" && a.ChildEntity == "Violation")
on new { g.ViolationId, this.NOVId } equals new { a.ChildEntityId, a.ParentId }
where g.CaseId == x.CaseId
select g).Count() > 0
select x).FirstOrDefault();
this.TriggerMetaDataUpdate<Case>(_case);
this.TriggerMetaDataUpdate<InspectionResult>(this.InspectionResult);
Case\u Case=(在this.Context().casepository.GetAll()中从x开始)
其中(从g到x)违反
在此.Context().OneToManyRepository.GetAll()中加入a,其中(a=>a.ParentEntity==“注意”&&a.ChildEntity==“违反”)
在新的{g.ViolationId,this.NOVId}上等于新的{a.ChildEntityId,a.ParentId}
其中g.CaseId==x.CaseId
选择g).Count()>0
选择x).FirstOrDefault();
this.TriggerMetaDataUpdate(_case);
this.TriggerMetaDataUpdate(this.InspectionResult);
我是通过以下方式完成的:
var Violations = (from v in UnitOfWork.ViolationRepository.GetAll()
join a in UnitOfWork.OneToManyRepository.GetAll().Where(a => a.ParentEntity == "Notice" && a.ChildEntity == "Violation")
on v.ViolationId equals a.ChildEntityId
join b in UnitOfWork.OneToManyRepository.GetAll().Where(a => a.ParentEntity == "Notice" && a.ChildEntity == "Case")
on a.ParentId equals b.ParentId
where b.ChildEntityId == caseId
orderby v.ViolationId
select v).ToList();
我们很难在不了解相关类型的情况下提供帮助。如果你能提供更多的信息,或者最好是一个简单的例子,那将非常有帮助。如果可以从rest中提取内部查询(如果声明与
x
类型相同的变量),那么也会更简单,您应该能够以更简单的方式显示问题。我怀疑问题在于您使用的两个匿名类型具有不同的形状-一个具有ViolationId
和NOVId
的属性,另一个具有ChildEntityId
和ParentId
的属性。与Where子句中的join无关,我有语法错误吗?为什么它会给我语法错误?是的,连接在where
子句中。我的观点是,如果在where
子句之外进行相同的连接,那么在一半的代码中可能会显示相同的问题。这不是语法错误-问题不在于语法,而在于类型推断。正如我所说,我怀疑这是因为你的join通过使用不同形状的匿名类型有效地表示“join on this apple等于this orange”。。。但是,虽然有很多可能不相关的代码,并且没有足够的类型信息来说明什么是相关的,但我们无法分辨。equals的两边必须是相同的类型。当有成员在名称和类型上匹配时,匿名类型的类型相同。您在一侧有{ViolationId,NOVId}
,在另一侧有{ChildEntityId,ParentId}
,因此这将永远不起作用。在new{Id1=g.ViolationId,Id2=this.NOVId}上执行类似于的操作{Id1=a.ChildEntityId,Id2=a.ParentId}
,看看会发生什么。根据NetMage的评论,这看起来比在两侧使用相同的匿名类型所做的更改效率要低得多。问题是我们仍然不知道这些类型是什么,等等。。。所以给你具体的帮助真的很难。你是对的,绝对正确,但我在阅读你的答案之前就完成了它,仍然张贴来获取你的建议,非常感谢我的朋友。