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: " +
   c.contact_name);
 }
}

See Also

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

Community Additions

Show: