linqtoEntityInnerJoin我有一个名为Customer的实体,它具有三个属性:publicclassCustomer{publicvirtualGuidCompanyId;公共虚拟长Id;公共虚拟字符串名称;我还有一个名为Splitting的实体,它具有三个属性:publicclassSplitting{publicvirtuallongCustomerId;公共虚拟长Id;公共虚拟字符串名称;现在我需要编写一个方法来获取companyId和customerId。该方法应返回与companyId中特定customerId相关的拆分列表。像这样:publicIListget(GuidcompanyId,longcustomrId){varres=fromsinSplittingfromcinCustomer......如何继续?返回res.ToList();}varres=fromsinSplittingjoincinCustomerons.CustomerIdequalsc.Idwherec.Id==customrId&&c.CompanyId==companyIdselects;使用扩展方法:varres=Splitting.Join(Customer,s=>s.CustomerId,c=>c.Id,(s,c)=>new{s,c}).Where(sc=>sc.c.Id==userId&&sc.c.CompanyId==companyId).Select(sc=>sc.s);您可以在visualstudio中找到一大堆Linq示例。只需选择帮助->示例,然后解压缩Linq示例。打开linq示例解决方案,打开SampleQueries项目的LinqSamples.cs。您正在寻找的答案是方法Linq14:int[]numbersA={0,2,4,5,6,8,9};int[]numbersB={1,3,5,7,8};varpairs=fromainnumbersAfrombinnumbersB其中a不是100%确定这两个实体之间的关系,但是这里是:以上就是C#学习教程的全部内容:从linq到实体的内部连接,如果对大家有用并且需要了解更多C#学习教程,希望大家多多关注---IListres=(fromsin[datasource]wheres.Customer.CompanyID==[companyID]&&s.CustomerID==[customerID]选择s).ToList();IListres=[数据源].Splittings.Where(x=>x.Customer.CompanyID==[companyID]&&x.CustomerID==[customerID]).ToList();publicIListget(GuidcompanyId,longcustomrId){varres=fromcinCustomers_data_sourcewherec.CustomerId=customrId&&c.CompanyID==companyIdfromsinSplittings_data_sroucewheres.CustomerID=c.CustomerIDselects;返回res.ToList();}本文收集自网络,不代表立场。如涉及侵权,请点击右侧联系管理员删除。如需转载请注明出处:
