Question on Entity Reference vs N:1 in LINQ

질문 Question on Entity Reference vs N:1 in LINQ

  • 2012년 4월 25일 수요일 오후 11:45
     
     

    I have writing some linq queries based on the OrganizationContext that I generate when I run the entity generator.

    I notice that sometimes I have to join on an N:1 between two entities and other times I have to use the Entity Reference object.  Is there a hard and fast rule to determine

    when one must be used as opposed to another ?  Sometimes I have to join on "Id" and other times it sthe entityId.Value. 

    Thanks,

모든 응답

  • 2012년 4월 26일 목요일 오후 12:15
    답변자
     
     

    Hi MRUNKS,

    The rule is that if you are joining on the Primary Key, it will be a Guid - so you use Id - but if you are joining on a foreign key (e.g. a N:1 relationship) you will need to use the EntityReference. For N:N Relationships, you use the intersection entity which has two foreign key lookups on it - so each will be an EntityReference.

    hth,

    Scott


    Scott Durow
    Read my blog: www.develop1.net/public
    If this post answers your question, please click "Mark As Answer" on the post and "Mark as Helpful"

  • 2012년 4월 26일 목요일 오후 1:30
     
     

    Thanks for the reply.

    I have another similar problem with joins. 

    the following set of joins works:  but if I move the last join to "unit of measure" to the top, I get the following Error message:
    System.InvalidOperationException: Sequence contains no matching element

    /**********************************************************
    Moving the join to unit of measure to the top breaks as commented out below.
    *********************************************************/

    from  analyticalResult in serviceContext.lims_analyticalresultsSet

    // Commented join to unit of measure since adding the join here breaks
    // join unitofmeasure in serviceContext.lims_unitofmeasureSet on analyticalResult.lims_uomid.Id equals unitofmeasure.Id

    join constituentMethod in serviceContext.lims_testconstituentmethodSet on analyticalResult.lims_analyticalresultstestconstituentmethod.Id equals constituentMethod.Id
    join constituent in serviceContext.lims_constituentSet on constituentMethod.lims_constituentid.Id equals constituent.Id
    join method in serviceContext.lims_methodSet on constituentMethod.lims_methodid.Id equals method.lims_methodId
    join sample in serviceContext.lims_sampleSet on analyticalResult.lims_sample.Id equals sample.lims_sampleId.Value
    join location in serviceContext.lims_locationSet on sample.lims_locationid.Id equals  location.Id
    join incident in serviceContext.IncidentSet on sample.lims_incidentid.Id equals incident.IncidentId
    join projects in serviceContext.lims_projectSet on incident.lims_projectid.Id equals projects.Id

    //Add join to unit of measure here and it works fine!
    join unitofmeasure in serviceContext.lims_unitofmeasureSet on analyticalResult.lims_uomid.Id equals unitofmeasure.Id

    I assume something in the provider is limited so the specific order matters.  Is there a way I can tell what the order should be ?

    Thanks,

  • 2012년 4월 27일 금요일 오전 7:49
    답변자
     
      코드 있음

    Hi MRUNKS,

    I've seen similar strange things with the CRM LINQ provider when using the Id field - rather than use the Id field, use the unique id schema name instead.

    E.g.

    join constituentMethod in serviceContext.lims_testconstituentmethodSet on analyticalResult.lims_analyticalresultstestconstituentmethod.Id equals constituentMethod.lims_testconstituentmethodId

    You would need to do the same for all places where you've use Id rather than the full schema name of the id. Id is simply a short cut for the full primary key, and the Linq provider doesn't seem to always be able to work out what the actual schema name is of the primary key when it compiles the query.

    A good way of testing linq query problems like this is to use LinqPad - or I wrote a little utility to convert Linq queries into QueryExpresisons and FetchXml (http://code.msdn.microsoft.com/Convert-CRM2011-Linq-into-93a163ee)

    hth,

    Scott


    Scott Durow
    Read my blog: www.develop1.net/public
    If this post answers your question, please click "Mark As Answer" on the post and "Mark as Helpful"