共用方式為


在 LINQ 中使用實體屬性的排列結果

 

發行︰ 2017年1月

適用於: Dynamics 365 (online)、Dynamics 365 (on-premises)、Dynamics CRM 2016、Dynamics CRM Online

在 Microsoft Dynamics 365 和 Microsoft Dynamics 365 (線上) 中,您可以使用查詢或 OptionSet (挑選清單) 屬性排列 LINQ 查詢中的結果。 本主題將說明這類查詢的幾個範例。

對 Order By 使用查詢值

下列範例將說明在 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

對 Order By 使用挑選清單

下列範例將說明使用查詢值做為排列依據 (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

另請參閱

建立與 LINQ (.NET Language Integrated Query) 的查詢
分頁 LINQ 大型結果集

Microsoft Dynamics 365

© 2017 Microsoft. 著作權所有,並保留一切權利。 著作權