使用LINQ添加第二个联接

使用LINQ添加第二个联接,linq,Linq,我有这个LINQ查询,我需要与它进行第二次连接: var linqQuery = (from r in gServiceContext.CreateQuery("opportunity") join c in gServiceContext.CreateQuery("contact") on ((EntityReference)r["new_contact"]).Id equals c["contactid"] into opp from o in opp.DefaultIf

我有这个LINQ查询,我需要与它进行第二次连接:

var linqQuery = (from r in gServiceContext.CreateQuery("opportunity")
    join c in gServiceContext.CreateQuery("contact") on ((EntityReference)r["new_contact"]).Id   equals c["contactid"] into opp
    from o in opp.DefaultIfEmpty()
    where ((EntityReference)r["new_channelpartner"]).Id.Equals(lProfileProperty.PropertyValue) && ((OptionSetValue)r["new_leadstatus"]).Equals("100000002")
    select new
但我也需要加入其中:

from r in gServiceContext.CreateQuery("annotation")
    join c in gServiceContext.CreateQuery("opportunity") on ((EntityReference)r["objectid"]).Id equals c["opportunityid"]
对不起,我相信这很容易。不过我在林克很差劲。任何帮助都会很好


谢谢

你的意思不太清楚,但我建议你:

var linqQuery = from r in gServiceContext.CreateQuery("opportunity")
                where ...
                join c in gServiceContext.CreateQuery("contact")
                   on  ((EntityReference)r["new_contact"]).Id
                   equals c["contactid"] into opp
                from o in opp.DefaultIfEmpty()
                join r in gServiceContext.CreateQuery("annotation")
                   on c["opportunityid"]
                   equals ((EntityReference)r["objectid"]).Id
                select new...
顺便说一句,不清楚为什么您必须到处强制转换到
EntityReference
,并通过字段名称作为字符串访问字段。您确定不能使用以下内容:

var linqQuery = from opportunity in gServiceContext.Opportunities
                where opportunity.ChannelPartner.Id == targetChannelPartner
                   && opportunity.NewLeadStatus == 100000002
                join contact in gServiceContext.Contacts
                  on opportunity.Id equals contact.Id into contacts
                from contactOrNull in contacts.DefaultIfEmpty()
                join annotation in gServiceContext.Annotations
                  on annotation.ObjectId equals opportunity.OpportunityId
                select ...;