将实体属性与 LINQ 结合使用的订单结果

 

发布日期: 2017年1月

适用于: Dynamics 365 (online),Dynamics 365 (on-premises),Dynamics CRM 2016,Dynamics CRM Online

在 Microsoft Dynamics 365 和 Microsoft Dynamics 365 (online) 中,可以使用查找或选项集(选择列表)属性在 LINQ 查询中对结果进行排序。 本主题显示一些此类查询的示例。

使用查找值指定排序依据

以下示例演示如何在 Order By 子句中使用查找属性 PrimaryContactId


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 (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

另请参阅

使用 LINQ(.NET 语言集成查询)构建查询
使用 LINQ 对大型结果集进行分页

Microsoft Dynamics 365

© 2017 Microsoft。 保留所有权利。 版权