使用LINQ添加第二个Join

时间:2011-12-29 19:44:56

标签: 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.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"]

抱歉,我确信这很容易。我虽然吮吸LINQ。任何帮助都会很棒。

谢谢!

1 个答案:

答案 0 :(得分:1)

你的意思并不是很清楚,但我建议你想要:

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 ...;