Use late-bound entity class with a LINQ query

 

Updated: November 29, 2016

Applies To: Dynamics 365 (online), Dynamics 365 (on-premises), Dynamics CRM 2016, Dynamics CRM Online

In Microsoft Dynamics 365 and Microsoft Dynamics 365 (online), you can use late binding with .NET Language-Integrated Query (LINQ) queries. Late binding uses the attribute logical name, and is resolved at runtime.

The following examples show how to use late binding in the join clause of a LINQ query.

Retrieve the full name of the contact that represents the primary contact for an account and the account name.



Using orgSvcContext As New OrganizationServiceContext(_serviceProxy)
 Dim query_join2 = From c In orgSvcContext.CreateQuery("contact") _
                   Join a In orgSvcContext.CreateQuery("account") _
                   On c("contactid") Equals a("primarycontactid") _
                   Select New With {Key .contact_name = c("fullname"),
                                    Key .account_name = a("name")}
 For Each c In query_join2
  Console.WriteLine(c.contact_name.ToString() & "  " _
                    & c.account_name.ToString())
 Next c
End Using

Retrieve Contact, Account and Lead data where the Lead was the originating Lead and the Contact’s last name is not “Parker”



Using orgSvcContext As New OrganizationServiceContext(_serviceProxy)
 Dim query_dejoin = From c In orgSvcContext.CreateQuery("contact") _
                    Join a In orgSvcContext.CreateQuery("account")
                    On c("contactid") Equals a("primarycontactid") _
                    Join l In orgSvcContext.CreateQuery("lead") _
                    On a("originatingleadid") Equals l("leadid") _
                    Select New With {Key .Contact = c,
                                     Key .Account = a,
                                     Key .Lead = l}
 For Each c In query_dejoin
  Console.WriteLine(c.Account.Attributes("name").ToString() _
                    & " " _
                    & c.Contact.Attributes("fullname").ToString() _
                    & " " _
                    & c.Lead.Attributes("leadid").ToString())
 Next c
End Using

The following example shows how to retrieve a list of Contact and Account information using a left join. A left join is designed to return parents with and without children from two sources. There is a correlation between parent and child, but no child may actually exist.



Using orgSvcContext As New OrganizationServiceContext(_serviceProxy)
 Dim query_join9 = From a In orgSvcContext.CreateQuery("account") _
                   Group Join c In orgSvcContext.CreateQuery("contact") _
                   On a("primarycontactid") Equals c("contactid") Into gr = _
                   Group From c_joined In gr.DefaultIfEmpty() _
                   Select New With {Key .account_name = a.Attributes("name")}
 For Each c In query_join9
  Console.WriteLine(c.account_name)
 Next c
End Using

The following example shows how to use late binding with the Contains method in a LINQ query.



Using orgSvcContext As New OrganizationServiceContext(_serviceProxy)
 Dim query_contains3 = From c In orgSvcContext.CreateQuery("contact") _
                       Where (CStr(c("description"))).Contains("Coho") _
                       Select New With
                             {Key .firstname = c.Attributes("firstname"),
                             Key .lastname = c.Attributes("lastname")}

 For Each c In query_contains3
  Console.WriteLine(c.firstname.ToString() & " " _
                    & c.lastname.ToString())
 Next c
End Using

The following example shows use of the not equals operator.



Using orgSvcContext As New OrganizationServiceContext(_serviceProxy)
 Dim query_ne3 = From c In orgSvcContext.CreateQuery("contact") _
                 Where (Not c("address1_city").Equals(Nothing)) _
                 Select New With {Key .FirstName = c("firstname"),
                                  Key .LastName = c("lastname"),
                                  Key .Address1_City =
                                  c("address1_city")}
 For Each c In query_ne3
  Console.WriteLine(c.FirstName.ToString() & " " _
                    & c.LastName.ToString() & " " _
                    & c.Address1_City.ToString())
 Next c
End Using

The following example shows how to retrieve Contact information using the GetAttributeValue method.



Using orgSvcContext As New OrganizationServiceContext(_serviceProxy)
 Dim list_getattrib1 = ( _
     From c In orgSvcContext.CreateQuery("contact") _
     Where Not c.GetAttributeValue(Of Guid?)("contactid") _
     .Value.Equals(_contactId1) _
     Select New With
            {
             Key .FirstName = c.GetAttributeValue(Of String)("firstname"),
             Key .LastName = c.GetAttributeValue(Of String)("lastname")}
         ).ToList()
 For Each c In list_getattrib1
  Console.WriteLine(c.FirstName & " " & c.LastName)
 Next c
End Using

Microsoft Dynamics 365

© 2016 Microsoft. All rights reserved. Copyright

Community Additions

ADD
Show: