LINQ クエリの例

 

公開日: 2017年1月

対象: Dynamics 365 (online)、Dynamics 365 (on-premises)、Dynamics CRM 2016、Dynamics CRM Online

ここでは、LINQ クエリの多数のサンプルを提供します。 完全なサンプルについては、「サンプル: 複雑な LINQ クエリ」を参照してください。

このトピックの内容

シンプルな Where 句

結合とシンプルな Where 句

Distinct 演算子の使用

シンプルな内部結合

自己結合

二重結合と複数結合

エンティティ フィールドを使用した結合

遅延バインド左結合

等価演算子の使用

非等価演算子の使用

Where 句によるメソッドベース LINQ クエリの使用

より大きい演算子の使用

以上または以下演算子の使用

"含む" 演算子の使用

"含まない" 演算子の使用

StartsWith および EndsWith 演算子の使用

And および Or 演算子の使用

OrderBy 演算子の使用

First および Single 演算子の使用

書式設定された値の取得

Skip および Take 演算子の使用 (ページングなし)

FirstOrDefault および SingleOrDefault 演算子の使用

リンクされたエンティティの条件に基づく自己結合の使用

Where 句での変換の使用

ページングの並べ替えの使用

関連するエンティティ列の取得 (一対多の関係の場合)

.value による属性値の取得

複数のプロジェクション (新しいデータ型を別の型にキャスト)

GetAttributeValue メソッドの使用

Math メソッドの使用

複数の Select 句と Where 句の使用

SelectMany の使用

文字列操作の使用

2 つの Where 句の使用

関連するレコードを取得するために LoadProperty を使用します。

シンプルな Where 句

次のサンプルは、Name に "Contoso" が含まれている取引先企業の一覧を取得する方法を示します。


using (ServiceContext svcContext = new ServiceContext(_serviceProxy))
{
 var query_where1 = from a in svcContext.AccountSet
                    where a.Name.Contains("Contoso")
                    select a;
 foreach (var a in query_where1)
 {
  System.Console.WriteLine(a.Name + " " + a.Address1_City);
 }
}

Using svcContext As New ServiceContext(_serviceProxy)
 Dim query_where1 = From c In svcContext.AccountSet _
                    Where c.Name.Contains("Contoso") _
                    Select c

 For Each c In query_where1
  Console.WriteLine(c.Name & " " & c.Address1_City)
 Next c
End Using

次のサンプルは、Name に "Contoso" が含まれ、Address1_City が “Redmond” である取引先企業の一覧を取得する方法を示します。


using (ServiceContext svcContext = new ServiceContext(_serviceProxy))
{
 var query_where2 = from a in svcContext.AccountSet
                    where a.Name.Contains("Contoso")
                    where a.Address1_City == "Redmond"
                    select a;

 foreach (var a in query_where2)
 {
  System.Console.WriteLine(a.Name + " " + a.Address1_City);
 }
}

Using svcContext As New ServiceContext(_serviceProxy)
 Dim query_where2 = From c In svcContext.AccountSet _
                    Where c.Name.Contains("Contoso") _
                    Where c.Address1_City.Equals("Redmond") _
                    Select c

 For Each c In query_where2
  Console.WriteLine(c.Name & " " & c.Address1_City)
 Next c
End Using

結合とシンプルな Where 句

次のサンプルは、取引先企業の Name に "Contoso" が含まれ、取引先担当者の LastName に "Smith" が含まれ、取引先担当者が取引先企業の取引先責任者である、取引先企業の Name と取引先担当者の LastName を取得する方法を示します。


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);
 }
}

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

Distinct 演算子の使用

次のサンプルは、取引先担当者の姓の個別一覧を取得する方法を示します。 重複がある名前でも、一覧には 1 回だけ出現します。


using (ServiceContext svcContext = new ServiceContext(_serviceProxy))
{
 var query_distinct = (from c in svcContext.ContactSet
                       select c.LastName).Distinct();
 foreach (var c in query_distinct)
 {
  System.Console.WriteLine(c);
 }
}

Using svcContext As New ServiceContext(_serviceProxy)
 Dim query_distinct = ( _
     From c In svcContext.ContactSet _
     Select c.LastName).Distinct()
 For Each c In query_distinct
  Console.WriteLine(c)
 Next c
End Using

シンプルな内部結合

次のサンプルは、取引先企業の取引先責任者の一覧に含まれる取引先企業および取引先担当者についての情報を取得する方法を示します。


using (ServiceContext svcContext = new ServiceContext(_serviceProxy))
{
 var query_join1 = from c in svcContext.ContactSet
                   join a in svcContext.AccountSet
                  on c.ContactId equals a.PrimaryContactId.Id
                   select new
                   {
                    c.FullName,
                    c.Address1_City,
                    a.Name,
                    a.Address1_Name
                   };
 foreach (var c in query_join1)
 {
  System.Console.WriteLine("acct: " +
   c.Name +
   "\t\t\t" +
   "contact: " +
   c.FullName);
 }
}

Using svcContext As New ServiceContext(_serviceProxy)
 Dim query_join1 = From c In svcContext.ContactSet _
                   Join a In svcContext.AccountSet _
                   On c.ContactId Equals a.account_primary_contact.Id _
                   Select New With {Key c.FullName, Key c.Address1_City,
                                    Key a.Name, Key a.Address1_Name}
 For Each c In query_join1
  Console.WriteLine("acct: " & c.Name & vbTab & vbTab _
                    & vbTab & "contact: " & c.FullName)
 Next c
End Using

自己結合

次のサンプルは、取引先企業の親会社である取引先企業についての情報を取得する方法を示します。


using (ServiceContext svcContext = new ServiceContext(_serviceProxy))
{
 var query_join5 = from a in svcContext.AccountSet
                   join a2 in svcContext.AccountSet
                   on a.ParentAccountId.Id equals a2.AccountId

                   select new
                   {
                    account_name = a.Name,
                    account_city = a.Address1_City
                   };
 foreach (var c in query_join5)
 {
  System.Console.WriteLine(c.account_name + "  " + c.account_city);
 }
}

Using svcContext As New ServiceContext(_serviceProxy)
 Dim query_join5 = From a In svcContext.AccountSet _
                   Join a2 In svcContext.AccountSet _
                   On a.ParentAccountId.Id Equals a2.AccountId _
                   Select New With {
                         Key .account_name = a.Name,
                         Key .account_city = a.Address1_City
                         }
 For Each c In query_join5
  Console.WriteLine(c.account_name & "  " & c.account_city)
 Next c
End Using

二重結合と複数結合

次のサンプルは、取引先担当者が取引先企業の取引先責任者であり、潜在顧客が取引先企業の元の潜在顧客である、取引先企業、取引先担当者、潜在顧客の情報を取得する方法を示します。


using (ServiceContext svcContext = new ServiceContext(_serviceProxy))
{
 var query_join4 = from a in svcContext.AccountSet
                   join c in svcContext.ContactSet
                   on a.PrimaryContactId.Id equals c.ContactId
                   join l in svcContext.LeadSet
                   on a.OriginatingLeadId.Id equals l.LeadId
                   select new
                   {
                    contact_name = c.FullName,
                    account_name = a.Name,
                    lead_name = l.FullName
                   };
 foreach (var c in query_join4)
 {
  System.Console.WriteLine(c.contact_name +
   "  " +
   c.account_name +
   "  " +
   c.lead_name);
 }
}

Using svcContext As New ServiceContext(_serviceProxy)
 Dim query_join4 = From a In svcContext.AccountSet _
                   Join c In svcContext.ContactSet _
                   On a.PrimaryContactId.Id Equals c.ContactId _
                   Join l In svcContext.LeadSet _
                   On a.OriginatingLeadId.Id Equals l.LeadId _
                   Select New With {Key .contact_name = c.FullName,
                                    Key .account_name = a.Name,
                                    Key .lead_name = l.FullName}
 For Each c In query_join4
  Console.WriteLine(c.contact_name & "  " & c.account_name _
                    & "  " & c.lead_name)
 Next c
End Using

次のサンプルは、取引先企業が別の取引先企業の親企業であり、取引先担当者がその取引先企業の取引先責任者である、取引先企業および取引先担当者の情報を取得する方法を示します。


using (ServiceContext svcContext = new ServiceContext(_serviceProxy))
{
 var query_join6 = from c in svcContext.ContactSet
                   join a in svcContext.AccountSet
                   on c.ContactId equals a.PrimaryContactId.Id
                   join a2 in svcContext.AccountSet
                   on a.ParentAccountId.Id equals a2.AccountId
                   select new
                   {
                    contact_name = c.FullName,
                    account_name = a.Name
                   };
 foreach (var c in query_join6)
 {
  System.Console.WriteLine(c.contact_name + "  " + c.account_name);
 }
}

Using svcContext As New ServiceContext(_serviceProxy)
 Dim query_join6 = From c In svcContext.ContactSet _
                   Join a In svcContext.AccountSet _
                   On c.ContactId Equals a.PrimaryContactId.Id _
                   Join a2 In svcContext.AccountSet _
                   On a.ParentAccountId.Id Equals a2.AccountId _
                   Select New With {Key .contact_name = c.FullName,
                                    Key .account_name = a.Name}
 For Each c In query_join6
  Console.WriteLine(c.contact_name & "  " & c.account_name)
 Next c
End Using

エンティティ フィールドを使用した結合

次のサンプルは、取引先企業の情報を一覧から取得する方法を示します


using (ServiceContext svcContext = new ServiceContext(_serviceProxy))
{
 var list_join = (from a in svcContext.AccountSet
                  join c in svcContext.ContactSet
                  on a.PrimaryContactId.Id equals c.ContactId
                  where a.Name == "Contoso Ltd" &&
                  a.Address1_Name == "Contoso Pharmaceuticals"
                  select a).ToList();
 foreach (var c in list_join)
 {
  System.Console.WriteLine("Account " + list_join[0].Name
      + " and it's primary contact "
      + list_join[0].PrimaryContactId.Id);
 }
}

Using svcContext As New ServiceContext(_serviceProxy)
 Dim list_join = ( _
     From a In svcContext.AccountSet _
     Join c In svcContext.ContactSet _
     On a.PrimaryContactId.Id Equals c.ContactId _
     Where a.Name.Equals("Contoso Ltd") _
     And a.Address1_Name.Equals("Contoso Pharmaceuticals") _
     Select a).ToList()
 For Each c In list_join
  Console.WriteLine("Account " & list_join(0).Name _
                    & " and it's primary contact " _
                    & list_join(0).PrimaryContactId.Id.ToString())
 Next c
End Using

遅延バインド左結合

次のサンプルは、左結合を示します。 左結合は、2 つのソースから子を持つ親と持たない親を返します。 親と子の間に関連付けがありますが、実際に子が存在するとは限りません。


using (ServiceContext svcContext = new ServiceContext(_serviceProxy))
{
 var query_join8 = from a in svcContext.AccountSet
                   join c in svcContext.ContactSet
                   on a.PrimaryContactId.Id equals c.ContactId
                   into gr
                   from c_joined in gr.DefaultIfEmpty()
                   select new
                   {
                    contact_name = c_joined.FullName,
                    account_name = a.Name
                   };
 foreach (var c in query_join8)
 {
  System.Console.WriteLine(c.contact_name + "  " + c.account_name);
 }
}

Using svcContext As New ServiceContext(_serviceProxy)
 Dim query_join8 = From a In svcContext.AccountSet _
                   Group Join c In svcContext.ContactSet _
                   On a.PrimaryContactId.Id Equals c.ContactId Into gr = _
                   Group From c_joined In gr.DefaultIfEmpty() _
                   Select New With {Key .contact_name = c_joined.FullName,
                                    Key .account_name = a.Name}
 For Each c In query_join8
  Console.WriteLine(c.contact_name & "  " & c.account_name)
 Next c
End Using

等価演算子の使用

次のサンプルは、FirstName が "Colin" である取引先担当者の一覧を取得する方法を示します。


using (ServiceContext svcContext = new ServiceContext(_serviceProxy))
{
 var query_equals1 = from c in svcContext.ContactSet
                     where c.FirstName.Equals("Colin")
                     select new
                     {
                      c.FirstName,
                      c.LastName,
                      c.Address1_City
                     };
 foreach (var c in query_equals1)
 {
  System.Console.WriteLine(c.FirstName +
   " " + c.LastName +
   " " + c.Address1_City);
 }
}

Using svcContext As New ServiceContext(_serviceProxy)
 Dim query_equals1 = From c In svcContext.ContactSet _
                     Where c.FirstName.Equals("Colin") _
                     Select New With {Key c.FirstName,
                                      Key c.LastName,
                                      Key c.Address1_City}
 For Each c In query_equals1
  Console.WriteLine(c.FirstName & " " & c.LastName & " " _
                    & c.Address1_City)
 Next c
End Using

次のサンプルは、FamilyStatusCode が "3" である取引先担当者の一覧を取得する方法を示します。 これは、離婚婚姻区分 オプションに相応します。


using (ServiceContext svcContext = new ServiceContext(_serviceProxy))
{
 var query_equals2 = from c in svcContext.ContactSet
                     where c.FamilyStatusCode.Equals(3)
                     select new
                     {
                      c.FirstName,
                      c.LastName,
                      c.Address1_City
                     };
 foreach (var c in query_equals2)
 {
  System.Console.WriteLine(c.FirstName +
   " " + c.LastName +
   " " + c.Address1_City);
 }
}

Using svcContext As New ServiceContext(_serviceProxy)
 Dim query_equals2 = From c In svcContext.ContactSet _
                     Where c.FamilyStatusCode.Equals(3) _
                     Select New With {Key c.FirstName,
                                      Key c.LastName,
                                      Key c.Address1_City}
 For Each c In query_equals2
  Console.WriteLine(c.FirstName & " " & c.LastName & " " _
                    & c.Address1_City)
 Next c
End Using

非等価演算子の使用

次のサンプルは、Address1_City が "Redmond" でない取引先担当者の一覧を取得する方法を示します。


using (ServiceContext svcContext = new ServiceContext(_serviceProxy))
{
 var query_ne1 = from c in svcContext.ContactSet
                 where c.Address1_City != "Redmond"
                 select new
                 {
                  c.FirstName,
                  c.LastName,
                  c.Address1_City
                 };
 foreach (var c in query_ne1)
 {
  System.Console.WriteLine(c.FirstName + " " +
   c.LastName + " " + c.Address1_City);
 }
}

Using svcContext As New ServiceContext(_serviceProxy)
 Dim query_ne1 = From c In svcContext.ContactSet _
                 Where c.Address1_City IsNot "Redmond" _
                 Select New With {Key c.FirstName,
                                  Key c.LastName,
                                  Key c.Address1_City}
 For Each c In query_ne1
  Console.WriteLine(c.FirstName & " " & c.LastName _
                    & " " & c.Address1_City)
 Next c
End Using

次のサンプルは、FirstName が "Colin" でない取引先担当者の一覧を取得する方法を示します。


using (ServiceContext svcContext = new ServiceContext(_serviceProxy))
{
 var query_ne2 = from c in svcContext.ContactSet
                 where !c.FirstName.Equals("Colin")
                 select new
                 {
                  c.FirstName,
                  c.LastName,
                  c.Address1_City
                 };

 foreach (var c in query_ne2)
 {
  System.Console.WriteLine(c.FirstName + " " +
   c.LastName + " " + c.Address1_City);
 }
}

Using svcContext As New ServiceContext(_serviceProxy)
 Dim query_ne2 = From c In svcContext.ContactSet _
                 Where (Not c.FirstName.Equals("Colin")) _
                 Select New With {Key c.FirstName,
                                  Key c.LastName,
                                  Key c.Address1_City}

 For Each c In query_ne2
  Console.WriteLine(c.FirstName & " " & c.LastName & " " _
                    & c.Address1_City)
 Next c
End Using

Where 句によるメソッドベース LINQ クエリの使用

次のサンプルは、LastName に "Smith" または “Smi” が含まれている取引先担当者の一覧を取得する方法を示します。


using (ServiceContext svcContext = new ServiceContext(_serviceProxy))
{
 var methodResults = svcContext.ContactSet
  .Where(a => a.LastName == "Smith");
 var methodResults2 = svcContext.ContactSet
  .Where(a => a.LastName.StartsWith("Smi"));
 Console.WriteLine();
 Console.WriteLine("Method query using Lambda expression");
 Console.WriteLine("---------------------------------------");
 foreach (var a in methodResults)
 {
  Console.WriteLine("Name: " + a.FirstName + " " + a.LastName);
 }
 Console.WriteLine("---------------------------------------");
 Console.WriteLine("Method query 2 using Lambda expression");
 Console.WriteLine("---------------------------------------");
 foreach (var a in methodResults2)
 {
  Console.WriteLine("Name: " + a.Attributes["firstname"] +
   " " + a.Attributes["lastname"]);
 }
}

Using svcContext As New ServiceContext(_serviceProxy)
 Dim methodResults = svcContext.ContactSet _
                     .Where(Function(a) a.LastName.Equals("Smith"))
 Dim methodResults2 = svcContext.ContactSet _
                      .Where(Function(a) a.LastName.StartsWith("Smi"))
 Console.WriteLine()
 Console.WriteLine("Method query using Lambda expression")
 Console.WriteLine("---------------------------------------")
 For Each a In methodResults
  Console.WriteLine("Name: " & a.FirstName & " " & a.LastName)
 Next a
 Console.WriteLine("---------------------------------------")
 Console.WriteLine("Method query 2 using Lambda expression")
 Console.WriteLine("---------------------------------------")
 For Each a In methodResults2
  Console.WriteLine("Name: " & a.Attributes("firstname").ToString() _
                    & " " & a.Attributes("lastname").ToString())
 Next a
End Using

より大きい演算子の使用

次のサンプルは、Anniversary の日付が 2010 年 2 月 5 日以降の取引先担当者の一覧を取得する方法を示します。


using (ServiceContext svcContext = new ServiceContext(_serviceProxy))
{
 var query_gt1 = from c in svcContext.ContactSet
                 where c.Anniversary > new DateTime(2010, 2, 5)
                 select new
                 {
                  c.FirstName,
                  c.LastName,
                  c.Address1_City
                 };

 foreach (var c in query_gt1)
 {
  System.Console.WriteLine(c.FirstName + " " +
   c.LastName + " " + c.Address1_City);
 }
}

Using svcContext As New ServiceContext(_serviceProxy)
 Dim query_gt1 = From c In svcContext.ContactSet _
                 Where c.Anniversary > New Date(2010, 2, 5) _
                 Select New With {Key c.FirstName,
                                  Key c.LastName,
                                  Key c.Address1_City}

 For Each c In query_gt1
  Console.WriteLine(c.FirstName & " " & c.LastName _
                    & " " & c.Address1_City)
 Next c
End Using

次のサンプルは、CreditLimit が $20,000 より大きい取引先担当者を取得する方法を示します。


using (ServiceContext svcContext = new ServiceContext(_serviceProxy))
{
 var query_gt2 = from c in svcContext.ContactSet
                 where c.CreditLimit.Value > 20000
                 select new
                 {
                  c.FirstName,
                  c.LastName,
                  c.Address1_City
                 };
 foreach (var c in query_gt2)
 {
  System.Console.WriteLine(c.FirstName + " " +
   c.LastName + " " + c.Address1_City);
 }
}

Using svcContext As New ServiceContext(_serviceProxy)
 Dim query_gt2 = From c In svcContext.ContactSet _
                 Where c.CreditLimit.Value > 20000 _
                 Select New With {Key c.FirstName,
                                  Key c.LastName,
                                  Key c.Address1_City}
 For Each c In query_gt2
  Console.WriteLine(c.FirstName & " " & c.LastName _
                    & " " & c.Address1_City)
 Next c
End Using

以上または以下演算子の使用

次のサンプルは、CreditLimit が $200 より大きく $400 未満の取引先担当者を取得する方法を示します。


using (ServiceContext svcContext = new ServiceContext(_serviceProxy))
{
 var query_gele1 = from c in svcContext.ContactSet
                   where c.CreditLimit.Value >= 200 &&
                   c.CreditLimit.Value <= 400
                   select new
                   {
                    c.FirstName,
                    c.LastName
                   };
 foreach (var c in query_gele1)
 {
  System.Console.WriteLine(c.FirstName + " " + c.LastName);
 }
}

Using svcContext As New ServiceContext(_serviceProxy)
 Dim query_gele1 = From c In svcContext.ContactSet _
                   Where c.CreditLimit.Value >= 200 _
                   AndAlso c.CreditLimit.Value <= 400 _
                   Select New With {Key c.FirstName,
                                    Key c.LastName}
 For Each c In query_gele1
  Console.WriteLine(c.FirstName &amp; " " &amp; c.LastName)
 Next c
End Using

"含む" 演算子の使用

次のサンプルは、Description が "Alpine" である取引先担当者の一覧を取得する方法を示します。


using (ServiceContext svcContext = new ServiceContext(_serviceProxy))
{
 var query_contains1 = from c in svcContext.ContactSet
                       where c.Description.Contains("Alpine")
                       select new
                       {
                        c.FirstName,
                        c.LastName
                       };
 foreach (var c in query_contains1)
 {
  System.Console.WriteLine(c.FirstName + " " + c.LastName);
 }
}

Using svcContext As New ServiceContext(_serviceProxy)
 Dim query_contains1 = From c In svcContext.ContactSet _
                       Where c.Description.Contains("Alpine") _
                       Select New With {Key c.FirstName,
                                        Key c.LastName}
 For Each c In query_contains1
  Console.WriteLine(c.FirstName &amp; " " &amp; c.LastName)
 Next c
End Using

"含まない" 演算子の使用

次のサンプルは、Description が "Coho" でない取引先担当者の一覧を取得する方法を示します。


using (ServiceContext svcContext = new ServiceContext(_serviceProxy))
{
 var query_contains2 = from c in svcContext.ContactSet
                       where !c.Description.Contains("Coho")
                       select new
                       {
                        c.FirstName,
                        c.LastName
                       };
 foreach (var c in query_contains2)
 {
  System.Console.WriteLine(c.FirstName + " " + c.LastName);
 }
}

Using svcContext As New ServiceContext(_serviceProxy)
 Dim query_contains2 = From c In svcContext.ContactSet _
                       Where (Not c.Description.Contains("Coho")) _
                       Select New With {Key c.FirstName,
                                        Key c.LastName}
 For Each c In query_contains2
  Console.WriteLine(c.FirstName.ToString() &amp; " " _
                    &amp; c.LastName.ToString())
 Next c
End Using

StartsWith および EndsWith 演算子の使用

次のサンプルは、FirstName が “Bri” で始まる取引先担当者を取得する方法を示します。


using (ServiceContext svcContext = new ServiceContext(_serviceProxy))
{
 var query_startswith1 = from c in svcContext.ContactSet
                         where c.FirstName.StartsWith("Bri")
                         select new
                         {
                          c.FirstName,
                          c.LastName
                         };
 foreach (var c in query_startswith1)
 {
  System.Console.WriteLine(c.FirstName + " " + c.LastName);
 }
}

Using svcContext As New ServiceContext(_serviceProxy)
 Dim query_startswith1 = From c In svcContext.ContactSet _
                         Where c.FirstName.StartsWith("Bri") _
                         Select New With {Key c.FirstName,
                                          Key c.LastName}
 For Each c In query_startswith1
  Console.WriteLine(c.FirstName.ToString() &amp; " " _
                    &amp; c.LastName.ToString())
 Next c
End Using

次のサンプルは、LastName が “cox” で終わる取引先担当者を取得する方法を示します。


using (ServiceContext svcContext = new ServiceContext(_serviceProxy))
{
 var query_endswith1 = from c in svcContext.ContactSet
                       where c.LastName.EndsWith("cox")
                       select new
                       {
                        c.FirstName,
                        c.LastName
                       };
 foreach (var c in query_endswith1)
 {
  System.Console.WriteLine(c.FirstName + " " + c.LastName);
 }
}

Using svcContext As New ServiceContext(_serviceProxy)
 Dim query_endswith1 = From c In svcContext.ContactSet _
                       Where c.LastName.EndsWith("cox") _
                       Select New With {Key c.FirstName,
                                        Key c.LastName}
 For Each c In query_endswith1
  Console.WriteLine(c.FirstName.ToString() &amp; " " _
                    &amp; c.LastName.ToString())
 Next c
End Using

And および Or 演算子の使用

次のサンプルは、Address1_City が "Redmond" または "Bellevue" で、かつ CreditLimit が $200 より大きい取引先担当者を取得する方法を示します。


using (ServiceContext svcContext = new ServiceContext(_serviceProxy))
{
 var query_andor1 = from c in svcContext.ContactSet
                    where ((c.Address1_City == "Redmond" ||
                    c.Address1_City == "Bellevue") &amp;&amp;
                    (c.CreditLimit.Value != null &amp;&amp;
                    c.CreditLimit.Value >= 200))
                    select c;

 foreach (var c in query_andor1)
 {
  System.Console.WriteLine(c.LastName + ", " + c.FirstName + " " +
   c.Address1_City + " " + c.CreditLimit.Value);
 }
}

Using svcContext As New ServiceContext(_serviceProxy)
 Dim query_andor1 = From c In svcContext.ContactSet _
                    Where c.Address1_City.Equals("Redmond") _
                    OrElse c.Address1_City.Equals("Bellevue") _
                    AndAlso c.CreditLimit.Value >= 200 _
                    Select New With {Key c.FirstName,
                                     Key c.LastName,
                                     Key c.CreditLimit,
                                     Key c.Address1_City}
 For Each c In query_andor1
  Console.WriteLine(c.LastName.ToString() &amp; ", " _
                    &amp; c.FirstName.ToString() &amp; " " _
                    &amp; c.Address1_City.ToString() &amp; " " _
                    &amp; c.CreditLimit.Value.ToString())
 Next c
End Using

OrderBy 演算子の使用

次のサンプルは、CreditLimit の降順で並べ替えた取引先担当者を取得する方法を示します。


using (ServiceContext svcContext = new ServiceContext(_serviceProxy))
{
 var query_orderby1 = from c in svcContext.ContactSet
                      where !c.CreditLimit.Equals(null)
                      orderby c.CreditLimit descending
                      select new
                      {
                       limit = c.CreditLimit,
                       first = c.FirstName,
                       last = c.LastName
                      };
 foreach (var c in query_orderby1)
 {
  System.Console.WriteLine(c.limit.Value + " " +
   c.last + ", " + c.first);
 }
}

Using svcContext As New ServiceContext(_serviceProxy)
 Dim query_orderby1 = From c In svcContext.ContactSet _
                      Where (Not c.CreditLimit.Equals(Nothing)) _
                      Order By c.CreditLimit Descending _
                      Select New With {Key .limit = c.CreditLimit,
                                       Key .first = c.FirstName,
                                       Key .last = c.LastName}
 For Each c In query_orderby1
  Console.WriteLine(c.limit.Value &amp; " " &amp; c.last &amp; ", " _
                    &amp; c.first)
 Next c
End Using

次のサンプルは、LastName を降順で、FirstName を昇順で並べ替えた取引先担当者を取得する方法を示します。


using (ServiceContext svcContext = new ServiceContext(_serviceProxy))
{
 var query_orderby2 = from c in svcContext.ContactSet
                      orderby c.LastName descending,
                      c.FirstName ascending
                      select new
                      {
                       first = c.FirstName,
                       last = c.LastName
                      };

 foreach (var c in query_orderby2)
 {
  System.Console.WriteLine(c.last + ", " + c.first);
 }
}

Using svcContext As New ServiceContext(_serviceProxy)
 Dim query_orderby2 = From c In svcContext.ContactSet _
                      Order By c.LastName Descending, _
                      c.FirstName Ascending _
                      Select New With {Key .first = c.FirstName,
                                       Key .last = c.LastName}

 For Each c In query_orderby2
  Console.WriteLine(c.last &amp; ", " &amp; c.first)
 Next c
End Using

First および Single 演算子の使用

次のサンプルは、返される最初の取引先担当者レコードだけを取得する方法、および条件に一致する取引先担当者レコードを 1 つだけ取得する方法を示します。


using (ServiceContext svcContext = new ServiceContext(_serviceProxy))
{
 Contact firstcontact = svcContext.ContactSet.First();

 Contact singlecontact = svcContext.ContactSet.Single(c => c.ContactId == _contactId1);
 System.Console.WriteLine(firstcontact.LastName + ", " +
  firstcontact.FirstName + " is the first contact");
 System.Console.WriteLine("==========================");
 System.Console.WriteLine(singlecontact.LastName + ", " +
  singlecontact.FirstName + " is the single contact");
}

Using svcContext As New ServiceContext(_serviceProxy)
 Dim firstcontact As Contact = svcContext.ContactSet _
                               .First()
 Console.WriteLine(firstcontact.LastName &amp; ", " _
                   &amp; firstcontact.FirstName _
                   &amp; " is the first contact")
 Console.WriteLine("==========================")
 Dim singlecontact As Contact =
     svcContext.ContactSet _
     .Single(Function(c) c.ContactId.Value.Equals(_contactId1))
 Console.WriteLine(singlecontact.LastName &amp; ", " _
                   &amp; singlecontact.FirstName _
                   &amp; " is the single contact")
End Using

書式設定された値の取得

次のサンプルは、optionset オプションのラベル (この場合は現在のレコード ステータスの値) を取得する方法を示します。


using (ServiceContext svcContext = new ServiceContext(_serviceProxy))
{
 var list_retrieve1 = from c in svcContext.ContactSet
                      where c.ContactId == _contactId1
                      select new { StatusReason = c.FormattedValues["statuscode"] };
 foreach (var c in list_retrieve1)
 {
  System.Console.WriteLine("Status: " + c.StatusReason);
 }
}

Using svcContext As New ServiceContext(_serviceProxy)
 Dim list_retrieve1 = From c In svcContext.ContactSet _
                      Where c.ContactId.Value.Equals(_contactId1) _
                      Select New With
                             {Key .StatusReason =
                                 c.FormattedValues("statuscode")}
 For Each c In list_retrieve1
  Console.WriteLine("Status: " &amp; c.StatusReason)
 Next c
End Using

Skip および Take 演算子の使用 (ページングなし)

次のサンプルは、Skip および Take 演算子を使用して、LastName が "Parker" ではないレコードを 2 つスキップした後で 2 レコードだけ取得する方法を示します。


using (ServiceContext svcContext = new ServiceContext(_serviceProxy))
{

 var query_skip = (from c in svcContext.ContactSet
                   where c.LastName != "Parker"
                   orderby c.FirstName
                   select new
                       {
                        last = c.LastName,
                        first = c.FirstName
                       }).Skip(2).Take(2);
 foreach (var c in query_skip)
 {
  System.Console.WriteLine(c.first + " " + c.last);
 }
}

Using svcContext As New ServiceContext(_serviceProxy)
 Dim query_skip = ( _
     From c In svcContext.ContactSet _
     Where c.LastName IsNot "Parker" _
     Order By c.FirstName _
     Select New With {Key .last = c.LastName,
                      Key .first = c.FirstName}).Skip(2).Take(2)
 For Each c In query_skip
  Console.WriteLine(c.first &amp; " " &amp; c.last)
 Next c
End Using

FirstOrDefault および SingleOrDefault 演算子の使用

FirstOrDefault 演算子は、シーケンスの最初の要素を返し、要素が見つからない場合は既定値を返します。SingleOrDefault 演算子は、シーケンスの特定の要素を 1 つだけ返し、その要素が見つからない場合は既定値を返します。 次のサンプルは、これらの演算子の使用方法を示します。


using (ServiceContext svcContext = new ServiceContext(_serviceProxy))
{

 Contact firstorcontact = svcContext.ContactSet.FirstOrDefault();

 Contact singleorcontact = svcContext.ContactSet
  .SingleOrDefault(c => c.ContactId == _contactId1);


 System.Console.WriteLine(firstorcontact.FullName +
  " is the first contact");
 System.Console.WriteLine("==========================");
 System.Console.WriteLine(singleorcontact.FullName +
  " is the single contact");
}

Using svcContext As New ServiceContext(_serviceProxy)
 Dim firstorcontact As Contact =
     svcContext.ContactSet.FirstOrDefault()
 Console.WriteLine(firstorcontact.FullName &amp; " is the first contact")

 Console.WriteLine("==========================")
 Dim singleorcontact As Contact =
     svcContext.ContactSet _
     .SingleOrDefault(Function(c) c.ContactId.Value.Equals(_contactId1))
 Console.WriteLine(singleorcontact.FullName &amp; " is the single contact")
End Using

リンクされたエンティティの条件に基づく自己結合の使用

次のサンプルは、一方の取引先企業が他方の取引先企業の親会社である 2 つの取引先企業の名前を取得する方法を示します。


using (ServiceContext svcContext = new ServiceContext(_serviceProxy))
{
 var query_joincond = from a1 in svcContext.AccountSet
                      join a2 in svcContext.AccountSet
                      on a1.ParentAccountId.Id equals a2.AccountId
                      where a2.AccountId == _accountId1
                      select new { Account = a1, Parent = a2 };
 foreach (var a in query_joincond)
 {
  System.Console.WriteLine(a.Account.Name + " " + a.Parent.Name);
 }
}

Using svcContext As New ServiceContext(_serviceProxy)
 Dim query_joincond = From a1 In svcContext.AccountSet _
                      Join a2 In svcContext.AccountSet _
                      On a1.ParentAccountId.Id Equals a2.AccountId _
                      Where a2.AccountId.Value.Equals(_accountId1) _
                      Select New With {Key .Account = a1,
                                       Key .Parent = a2}
 For Each a In query_joincond
  Console.WriteLine(a.Account.Name &amp; " " &amp; a.Parent.Name)
 Next a
End Using

Where 句での変換の使用

次のサンプルは、記念日の日付が 2010 年 1 月 1 日より後である特定の取引先担当者を取得する方法を示します。


using (ServiceContext svcContext = new ServiceContext(_serviceProxy))
{
 var query_wheretrans = from c in svcContext.ContactSet
                        where c.ContactId == _contactId1 &amp;&amp;
                        c.Anniversary > DateTime.Parse("1/1/2010")
                        select new
                        {
                         c.FirstName,
                         c.LastName
                        };
 foreach (var c in query_wheretrans)
 {
  System.Console.WriteLine(c.FirstName + " " + c.LastName);
 }
}

Using svcContext As New ServiceContext(_serviceProxy)
 Dim query_wheretrans = From c In svcContext.ContactSet _
                        Where c.ContactId.Value.Equals(_contactId1) _
                        AndAlso c.Anniversary > Date.Parse("1/1/2010") _
                        Select New With {Key c.FirstName,
                                         Key c.LastName}
 For Each c In query_wheretrans
  Console.WriteLine(c.FirstName &amp; " " &amp; c.LastName)
 Next c
End Using

ページングの並べ替えの使用

次のサンプルは、追加条件を使用して複数の列を並べ替える方法を示します。


using (ServiceContext svcContext = new ServiceContext(_serviceProxy))
{
 var query_pagingsort1 = (from c in svcContext.ContactSet
                          where c.LastName != "Parker"
                          orderby c.LastName ascending,
                          c.FirstName descending
                          select new { c.FirstName, c.LastName })
                          .Skip(2).Take(2);
 foreach (var c in query_pagingsort1)
 {
  System.Console.WriteLine(c.FirstName + " " + c.LastName);
 }
}

Using svcContext As New ServiceContext(_serviceProxy)
 Dim query_pagingsort1 = ( _
     From c In svcContext.ContactSet _
     Where c.LastName IsNot "Parker" _
     Order By c.LastName Ascending, c.FirstName Descending _
     Select New With {Key c.FirstName,
                      Key c.LastName}).Skip(2).Take(2)
 For Each c In query_pagingsort1
  Console.WriteLine(c.FirstName &amp; " " &amp; c.LastName)
 Next c
End Using

次のサンプルは、並べ替えに使用する列が取得する列とは異なる場合のページングの並べ替えを示します。


using (ServiceContext svcContext = new ServiceContext(_serviceProxy))
{
 var query_pagingsort2 = (from c in svcContext.ContactSet
                          where c.LastName != "Parker"
                          orderby c.FirstName descending
                          select new { c.FirstName }).Skip(2).Take(2);
 foreach (var c in query_pagingsort2)
 {
  System.Console.WriteLine(c.FirstName);
 }
}

Using svcContext As New ServiceContext(_serviceProxy)
 Dim query_pagingsort2 = ( _
     From c In svcContext.ContactSet _
     Where c.LastName IsNot "Parker" _
     Order By c.FirstName Descending _
     Select New With {Key c.FirstName}).Skip(2).Take(2)
 For Each c In query_pagingsort2
  Console.WriteLine(c.FirstName)
 Next c
End Using

次のサンプルは、最初の 10 件のレコードを取得する方法を示します。


using (ServiceContext svcContext = new ServiceContext(_serviceProxy))
{
 var query_pagingsort3 = (from c in svcContext.ContactSet
                          where c.LastName.StartsWith("W")
                          orderby c.MiddleName ascending,
                          c.FirstName descending
                          select new
                          {
                           c.FirstName,
                           c.MiddleName,
                           c.LastName
                          }).Take(10);
 foreach (var c in query_pagingsort3)
 {
  System.Console.WriteLine(c.FirstName + " " +
   c.MiddleName + " " + c.LastName);
 }
}

Using svcContext As New ServiceContext(_serviceProxy)
 Dim query_pagingsort3 = ( _
     From c In svcContext.ContactSet _
     Where c.LastName.StartsWith("W") _
     Order By c.MiddleName Ascending, c.FirstName Descending _
     Select New With {Key c.FirstName,
                      Key c.MiddleName,
                      Key c.LastName}).Take(10)
 For Each c In query_pagingsort3
  Console.WriteLine(c.FirstName &amp; " " &amp; c.MiddleName &amp; " " &amp; c.LastName)
 Next c
End Using

関連するエンティティ列の取得 (一対多の関係の場合)

次のサンプルは、関連する取引先企業と取引先担当者のレコードから列を取得する方法を示します。


using (ServiceContext svcContext = new ServiceContext(_serviceProxy))
{
 var query_retrieve1 = from c in svcContext.ContactSet
                       join a in svcContext.AccountSet
                       on c.ContactId equals a.PrimaryContactId.Id
                       where c.ContactId != _contactId1
                       select new { Contact = c, Account = a };
 foreach (var c in query_retrieve1)
 {
  System.Console.WriteLine("Acct: " + c.Account.Name +
   "\t\t" + "Contact: " + c.Contact.FullName);
 }
}

Using svcContext As New ServiceContext(_serviceProxy)
 Dim query_retrieve1 = From c In svcContext.ContactSet _
                       Join a In svcContext.AccountSet _
                       On c.ContactId Equals a.PrimaryContactId.Id _
                       Where Not c.ContactId.Value.Equals(_contactId1) _
                       Select New With {Key .Contact = c,
                                        Key .Account = a}
 For Each c In query_retrieve1
  Console.WriteLine("Acct: " &amp; c.Account.Name &amp; vbTab &amp; vbTab _
                    &amp; "Contact: " &amp; c.Contact.FullName)
 Next c
End Using

.value による属性値の取得

次のサンプルは、属性の値にアクセスするための値の使用方法を示します。


using (ServiceContext svcContext = new ServiceContext(_serviceProxy))
{

 var query_value = from c in svcContext.ContactSet
                   where c.ContactId != _contactId2
                   select new
                   {
                    ContactId = c.ContactId != null ?
                     c.ContactId.Value : Guid.Empty,
                    NumberOfChildren = c.NumberOfChildren != null ?
                     c.NumberOfChildren.Value : default(int),
                    CreditOnHold = c.CreditOnHold != null ?
                     c.CreditOnHold.Value : default(bool),
                    Anniversary = c.Anniversary != null ?
                     c.Anniversary.Value : default(DateTime)
                   };

 foreach (var c in query_value)
 {
  System.Console.WriteLine(c.ContactId + " " + c.NumberOfChildren + 
   " " + c.CreditOnHold + " " + c.Anniversary);
 }
}

Using svcContext As New ServiceContext(_serviceProxy)
 Dim query_value = From c In svcContext.ContactSet _
                   Where Not c.ContactId.Value.Equals(_contactId2) _
                   Select New With
                          {Key .ContactId = If(
                                  c.ContactId IsNot Nothing,
                                  c.ContactId.Value,
                                  Guid.Empty),
                           Key .NumberOfChildren = If(
                               c.NumberOfChildren IsNot Nothing,
                               c.NumberOfChildren.Value, Nothing),
                           Key .CreditOnHold = If(
                               c.CreditOnHold IsNot Nothing,
                               c.CreditOnHold.Value,
                               Nothing),
                           Key .Anniversary = If(
                               c.Anniversary IsNot Nothing,
                               c.Anniversary.Value,
                               Nothing)}

 For Each c In query_value
  Console.WriteLine(c.ContactId.ToString() &amp; " " _
                    &amp; c.NumberOfChildren &amp; " " _
                    &amp; c.CreditOnHold &amp; " " &amp; c.Anniversary)
 Next c
End Using

複数のプロジェクション (新しいデータ型を別の型にキャスト)

次のサンプルは、複数のプロジェクションおよび値を異なるデータ型にキャストする方法を示します。


using (ServiceContext svcContext = new ServiceContext(_serviceProxy))
{
 var query_projections = from c in svcContext.ContactSet
                         where c.ContactId == _contactId1
                         &amp;&amp; c.NumberOfChildren != null &amp;&amp; 
                         c.Anniversary.Value != null
                         select new
                         {
                          Contact = new Contact { 
                           LastName = c.LastName, 
                           NumberOfChildren = c.NumberOfChildren 
                          },
                          NumberOfChildren = (double)c.NumberOfChildren,
                          Anniversary = c.Anniversary.Value.AddYears(1),
                         };
 foreach (var c in query_projections)
 {
  System.Console.WriteLine(c.Contact.LastName + " " + 
   c.NumberOfChildren + " " + c.Anniversary);
 }
}

Using svcContext As New ServiceContext(_serviceProxy)
 Dim query_projections = From c In svcContext.ContactSet _
                         Where c.ContactId.Value.Equals(_contactId1) _
                         And Not c.NumberOfChildren.Equals(Nothing) _
                         And Not c.Anniversary.Equals(Nothing) _
        Select New With
               {Key .Contact =
                   New Contact With
                   {.LastName = c.LastName,
                    .NumberOfChildren = c.NumberOfChildren},
                Key .NumberOfChildren = CDbl(c.NumberOfChildren),
                Key .Anniversary = c.Anniversary.Value.AddYears(1)}
 For Each c In query_projections
  Console.WriteLine(c.Contact.LastName &amp; " " &amp; c.NumberOfChildren _
                    &amp; " " &amp; c.Anniversary)
 Next c
End Using

GetAttributeValue メソッドの使用

次のサンプルは、GetAttributeValue<T> メソッドの使用方法を示します。


using (ServiceContext svcContext = new ServiceContext(_serviceProxy))
{
 var query_getattrib = from c in svcContext.ContactSet
                       where c.GetAttributeValue<Guid>("contactid") != _contactId1
                       select new
                       {
                        ContactId = c.GetAttributeValue<Guid?>("contactid"),
                        NumberOfChildren = c.GetAttributeValue<int?>("numberofchildren"),
                        CreditOnHold = c.GetAttributeValue<bool?>("creditonhold"),
                        Anniversary = c.GetAttributeValue<DateTime?>("anniversary"),
                       };

 foreach (var c in query_getattrib)
 {
  System.Console.WriteLine(c.ContactId + " " + c.NumberOfChildren + 
   " " + c.CreditOnHold + " " + c.Anniversary);
 }
}

Using svcContext As New ServiceContext(_serviceProxy)
 Dim query_getattrib = From c In svcContext.ContactSet _
                       Where Not c.GetAttributeValue(Of Guid)("contactid").Equals( _
                        _contactId1) _
                       Select New With
                              {Key .ContactId =
                                  c.GetAttributeValue(Of Guid?)("contactid"),
                               Key .NumberOfChildren =
                               c.GetAttributeValue(Of Integer?)("numberofchildren"),
                               Key .CreditOnHold =
                               c.GetAttributeValue(Of Boolean?)("creditonhold"),
                               Key .Anniversary =
                               c.GetAttributeValue(Of Date?)("anniversary")}

 For Each c In query_getattrib
  Console.WriteLine(c.ContactId.ToString() &amp; " " _
                    &amp; c.NumberOfChildren &amp; " " &amp; c.CreditOnHold _
                    &amp; " " &amp; c.Anniversary)
 Next c
End Using

Math メソッドの使用

次のサンプルは、各種 Math メソッドの使用方法を示します。


using (ServiceContext svcContext = new ServiceContext(_serviceProxy))
{
 var query_math = from c in svcContext.ContactSet
                  where c.ContactId != _contactId2
                  &amp;&amp; c.Address1_Latitude != null &amp;&amp; 
                  c.Address1_Longitude != null
                  select new
                  {
                   Round = Math.Round(c.Address1_Latitude.Value),
                   Floor = Math.Floor(c.Address1_Latitude.Value),
                   Ceiling = Math.Ceiling(c.Address1_Latitude.Value),
                   Abs = Math.Abs(c.Address1_Latitude.Value),
                  };
 foreach (var c in query_math)
 {
  System.Console.WriteLine(c.Round + " " + c.Floor + 
   " " + c.Ceiling + " " + c.Abs);
 }
}

Using svcContext As New ServiceContext(_serviceProxy)
 Dim query_math = From c In svcContext.ContactSet _
                  Where Not c.ContactId.Value.Equals(_contactId2) _
                  AndAlso c.Address1_Latitude IsNot Nothing _
                  AndAlso c.Address1_Longitude IsNot Nothing _
                  Select New With
                         {Key .Round =
                             Math.Round(c.Address1_Latitude.Value),
                          Key .Floor =
                             Math.Floor(c.Address1_Latitude.Value),
                          Key .Ceiling =
                             Math.Ceiling(c.Address1_Latitude.Value),
                          Key .Abs =
                             Math.Abs(c.Address1_Latitude.Value)}
 For Each c In query_math
  Console.WriteLine(c.Round &amp; " " &amp; c.Floor &amp; " " _
                    &amp; c.Ceiling &amp; " " &amp; c.Abs)
 Next c
End Using

複数の Select 句と Where 句の使用

次のサンプルは、メソッド ベースのクエリ構文を使用する複数の Select 句と Where 句を示します。


using (ServiceContext svcContext = new ServiceContext(_serviceProxy))
{
 var query_multiselect = svcContext.IncidentSet
                        .Where(i => i.IncidentId != _incidentId1)
                        .Select(i => i.incident_customer_accounts)
                        .Where(a => a.AccountId != _accountId2)
                        .Select(a => a.account_primary_contact)
                        .OrderBy(c => c.FirstName)
                        .Select(c => c.ContactId);
 foreach (var c in query_multiselect)
 {
  System.Console.WriteLine(c.GetValueOrDefault());
 }
}

Using svcContext As New ServiceContext(_serviceProxy)
 Dim query_multiselect =
     svcContext.IncidentSet _
     .Where(Function(i) Not i.IncidentId.Value.Equals(_incidentId1)) _
     .Select(Function(i) i.incident_customer_accounts) _
     .Where(Function(a) Not a.AccountId.Value.Equals(_accountId2)) _
     .Select(Function(a) a.account_primary_contact) _
     .OrderBy(Function(c) c.FirstName).Select(Function(c) c.ContactId)
 For Each c In query_multiselect
  Console.WriteLine(c.GetValueOrDefault())
 Next c
End Using

SelectMany の使用

次のサンプルは、SelectMany メソッドの使用方法を示します。


using (ServiceContext svcContext = new ServiceContext(_serviceProxy))
{
 var query_selectmany = svcContext.ContactSet
                        .Where(c => c.ContactId != _contactId2)
                        .SelectMany(c => c.account_primary_contact)
                        .OrderBy(a => a.Name);
 foreach (var c in query_selectmany)
 {
  System.Console.WriteLine(c.AccountId + " " + c.Name);
 }
}

Using svcContext As New ServiceContext(_serviceProxy)
 Dim query_selectmany =
     svcContext.ContactSet _
     .Where(Function(c) Not c.ContactId.Value.Equals(_contactId2)) _
     .SelectMany(Function(c) c.account_primary_contact) _
     .OrderBy(Function(a) a.Name)
 For Each c In query_selectmany
  Console.WriteLine(c.AccountId.ToString() &amp; " " &amp; c.Name)
 Next c
End Using

文字列操作の使用

次のサンプルは、各種文字列メソッドの使用方法を示します。


using (ServiceContext svcContext = new ServiceContext(_serviceProxy))
{
 var query_string = from c in svcContext.ContactSet
                    where c.ContactId == _contactId2
                    select new
                    {
                     IndexOf = c.FirstName.IndexOf("contact"),
                     Insert = c.FirstName.Insert(1, "Insert"),
                     Remove = c.FirstName.Remove(1, 1),
                     Substring = c.FirstName.Substring(1, 1),
                     ToUpper = c.FirstName.ToUpper(),
                     ToLower = c.FirstName.ToLower(),
                     TrimStart = c.FirstName.TrimStart(),
                     TrimEnd = c.FirstName.TrimEnd(),
                    };

 foreach (var c in query_string)
 {
  System.Console.WriteLine(c.IndexOf + "\n" + c.Insert + "\n" + 
   c.Remove + "\n" + c.Substring + "\n"
                           + c.ToUpper + "\n" + c.ToLower + 
                           "\n" + c.TrimStart + " " + c.TrimEnd);
 }
}

Using svcContext As New ServiceContext(_serviceProxy)
 Dim query_string = From c In svcContext.ContactSet _
                    Where c.ContactId.Value.Equals(_contactId2) _
                    Select New With
                           {Key .IndexOf = c.FirstName.IndexOf("contact"),
                            Key .Insert = c.FirstName.Insert(1, "Insert"),
                            Key .Remove = c.FirstName.Remove(1, 1),
                            Key .Substring = c.FirstName.Substring(1, 1),
                            Key .ToUpper = c.FirstName.ToUpper(),
                            Key .ToLower = c.FirstName.ToLower(),
                            Key .TrimStart = c.FirstName.TrimStart(),
                            Key .TrimEnd = c.FirstName.TrimEnd()}

 For Each c In query_string
  Console.WriteLine(c.IndexOf &amp; vbLf &amp; c.Insert &amp; vbLf _
                    &amp; c.Remove &amp; vbLf &amp; c.Substring &amp; vbLf _
                    &amp; c.ToUpper &amp; vbLf &amp; c.ToLower &amp; vbLf _
                    &amp; c.TrimStart &amp; " " &amp; c.TrimEnd)
 Next c
End Using

2 つの Where 句の使用

次のサンプルは、2 つの Where 句の使用方法を示します。


using (ServiceContext svcContext = new ServiceContext(_serviceProxy))
{
 var query_twowhere = from a in svcContext.AccountSet
                      join c in svcContext.ContactSet 
                      on a.PrimaryContactId.Id equals c.ContactId
                      where c.LastName == "Smith" &amp;&amp; c.CreditOnHold != null
                      where a.Name == "Contoso Ltd"
                      orderby a.Name
                      select a;
 foreach (var c in query_twowhere)
 {
  System.Console.WriteLine(c.AccountId + " " + c.Name);
 }
}

Using svcContext As New ServiceContext(_serviceProxy)
 Dim query_twowhere = From a In svcContext.AccountSet _
                      Join c In svcContext.ContactSet _
                      On a.PrimaryContactId.Id Equals c.ContactId _
                      Where c.LastName.Equals("Smith") _
                      AndAlso c.CreditOnHold IsNot Nothing _
                      Where a.Name.Equals("Contoso Ltd") _
                      Order By a.Name _
                      Select a
 For Each c In query_twowhere
  Console.WriteLine(c.AccountId.ToString() &amp; " " &amp; c.Name)
 Next c
End Using

関連するレコードを取得するために LoadProperty を使用します。

次のサンプルは関連レコードにアクセスする LoadProperty の方法を示します。


Contact benAndrews = svcContext.ContactSet.Where(c => c.FullName == "Ben Andrews").FirstOrDefault();
if (benAndrews != null)
{
 //benAndrews.Contact_Tasks is null until LoadProperty is used.
 svcContext.LoadProperty(benAndrews, "Contact_Tasks");
 Task benAndrewsFirstTask = benAndrews.Contact_Tasks.FirstOrDefault();
 if (benAndrewsFirstTask != null)
 {
  Console.WriteLine("Ben Andrews first task with Subject: '{0}' retrieved.", benAndrewsFirstTask.Subject);
 }
}

関連項目

LINQ (.NET 統合言語クエリ) を使用してクエリを作成する
サンプル: LINQ クエリの作成

Microsoft Dynamics 365

© 2017 Microsoft. All rights reserved. 著作権