Filter Multiple Entities with LINQ

[Applies to: Microsoft Dynamics CRM 2011]

You can create complex .NET Language-Integrated Query (LINQ) queries in Microsoft Dynamics CRM 2011. You use multiple Join clauses with filter clauses to create a result that is filtered on attributes from several entities.

Filter Multiple Entities Example

The following sample shows how to create a LINQ query that works with two entities and filters the result based on values from each of the entities.

using (ServiceContext svcContext = new ServiceContext(_serviceProxy))
 var query_where3 = from c in svcContext.ContactSet
                    join a in svcContext.AccountSet
                    on c.ContactId equals a.PrimaryContactId.Id
                    where a.Name.Contains("Contoso")
                    where c.LastName.Contains("Smith")
                    select new
                     account_name = a.Name,
                     contact_name = c.LastName

 foreach (var c in query_where3)
  System.Console.WriteLine("acct: " +
   c.account_name +
   "\t\t\t" +
   "contact: " +
Using svcContext As New ServiceContext(_serviceProxy)
 Dim query_where3 = From c In svcContext.ContactSet _
                    Join a In svcContext.AccountSet _
                    On c.ContactId Equals a.account_primary_contact.Id _
                    Where a.Name.Contains("Contoso") _
                    Where c.LastName.Contains("Smith") _
                    Select New With {Key .account_name = a.Name,
                                     Key .contact_name = c.LastName}

 For Each c In query_where3
  Console.WriteLine("acct: " & c.account_name & vbTab & vbTab _
                    & vbTab & "contact: " & c.contact_name)
 Next c
End Using

See Also


Sample: Complex LINQ Queries

Other Resources

Build Queries with LINQ (.NET Language-Integrated Query)

Microsoft Dynamics CRM 2011
Send comments about this topic to Microsoft.
© 2013 Microsoft Corporation. All rights reserved.