Share via


Order results using entity attributes with LINQ

 

Applies To: Dynamics CRM 2013

In Microsoft Dynamics CRM 2013 and Microsoft Dynamics CRM Online, you can use lookup or OptionSet (Picklist) attributes to order results within a LINQ query. This topic shows several examples of this type of query.

Using a Lookup Value to Order By

The following sample shows use the lookup attribute PrimaryContactId in an Order By clause.


using (ServiceContext svcContext = new ServiceContext(_serviceProxy))
{
 var query_orderbylookup = from a in svcContext.AccountSet
                           where a.Address1_Name == "Contoso Pharmaceuticals"
                           orderby a.PrimaryContactId
                           select new
                           {
                            a.Name,
                            a.Address1_City
                           };
 foreach (var a in query_orderbylookup)
 {
  System.Console.WriteLine(a.Name + " " + a.Address1_City);
 }
}

Using svcContext As New ServiceContext(_serviceProxy)
 Dim query_orderbylookup = From a In svcContext.AccountSet _
                           Where a.Address1_Name.Equals("Contoso Pharmaceuticals") _
                           Order By a.PrimaryContactId _
                           Select New With {Key a.Name,
                                            Key a.Address1_City}
 For Each a In query_orderbylookup
  Console.WriteLine(a.Name & " " & a.Address1_City)
 Next a
End Using

Using a Picklist to Order By

The following sample shows use of a lookup value to order by.


using (ServiceContext svcContext = new ServiceContext(_serviceProxy))
{
 var query_orderbypicklist = from c in svcContext.ContactSet
                             where c.LastName != "Parker" &&
                             c.AccountRoleCode != null
                             orderby c.AccountRoleCode, c.FirstName
                             select new
                             {
                              AccountRole = c.FormattedValues["accountrolecode"],
                              c.FirstName,
                              c.LastName
                             };
 foreach (var c in query_orderbypicklist)
 {
  System.Console.WriteLine(c.AccountRole + " " +
   c.FirstName + " " + c.LastName);
 }
}

Using svcContext As New ServiceContext(_serviceProxy)
 Dim query_orderbypicklist = From c In svcContext.ContactSet _
                             Where c.LastName IsNot "Parker" _
                             AndAlso c.AccountRoleCode IsNot Nothing _
                             Order By c.AccountRoleCode, c.FirstName _
                             Select New With
                                    {Key .AccountRole =
                                        c.FormattedValues("accountrolecode"),
                                     Key c.FirstName, Key c.LastName}
 For Each c In query_orderbypicklist
  Console.WriteLine(c.AccountRole & " " & c.FirstName _
                    & " " & c.LastName)
 Next c
End Using

See Also

Build queries with LINQ (.NET language-integrated query)
Page large result sets with LINQ