LINQ
LINQ มี 5 แบบคือ
LINQ to Object
LINQ to XML
LINQ to DataSet
LINQ to SQL
LINQ to Entities
Query Operator ใน LINQ มีหลายชนิดเช่น Select,Where,Join,GroupBy,OrderBy,ThenBy,OrderByDescending,ThenByDescending,Cast,GroupJoin,SelectMany
ตัวอย่างการ ใช้งาน Select เพื่อแสดงข้อมูลในตัวแปร array
using System;
using System.Linq;
public class TestLINQ
{
public static void Main()
{
string[] str = { "one","two","three","four","five" };
var q = from s in str select s;
foreach (var a in q)
{
Console.WriteLine(a);
}
Console.ReadLine();
}
}
ผลลัพธ์จะได้
one
two
three
four
five
การ ใช้ where เพื่อกำหนดเงื่อนไข
using System;
using System.Linq;
public class TestLINQ
{
public static void Main()
{
string[] str = {"one","two","three","four","five"};
var q =from s in str where s.Length < 4 select s; //ใช้ where เพื่อกำหนดเงื่อนไขวเพื่อให้แสดงสมาชิกที่มีความยาวน้อยว่า 4
foreach (var a in q)
{
Console.WriteLine(a);
}
Console.ReadLine();
}
}
ผลลัพธ์
one
two
คำอธิบายที่ได้ one และ two เพราะตรงตามเงื่อนไขที่มีความยาวของตัวอักษรน้อยกว่า 4 (where s.Length <4)
การใช้งาน OfType
using System;
using System.Linq;
using System.Collections.Generic;
public class Test
{
public static void Main()
{
string[] str = { "A", "B", "C" };
IEnumerable<string> i = str.OfType<string>();
foreach (string n in i)
{
Console.WriteLine(n);
}
Console.ReadLine();
}
}
ผลลัพธ์จะได้
A
B
C
การใช้งาน OrderBy
using System;
using System.Linq;
using System.Collections.Generic;
public class Test
{
public static void Main()
{
string[] str = {"Two","Six","Three","four","five","One"};
IEnumerable<string> i = from s in str where s.Length == 3 orderby s select s;
foreach (string a in i)
{
Console.WriteLine(a);
}
Console.ReadLine();
}
}
ผลลัพธ์จะได้
One
Six
Two
ในตัวอย่างนี้จะเป็นการเลือก สมาชิกในตัวแปร str ที่มีขนาดความยาวตัวอักษรเท่ากับ 3 โดยเรียงลำดับตัวอักษรจาก (a-z)
ตัวอย่างการใช้ where เพื่อเลือกข้อมูลที่มีขนาดความยาวตัวอักษรเท่ากับ 3
using System;
using System.Linq;
using System.Collections.Generic;
public class Test
{
public static void Main()
{
string[] str = {"Two","Six","Three","four","five","One"};
IEnumerable<string> i = str.Where(s => s.Length == 3);
foreach (string a in i)
{
Console.WriteLine(a);
}
Console.ReadLine();
}
}
ผลลัพธ์จะได้
Two
Six
One
การแสดงข้อความตามเงื่อนไขที่กำหนด
using System;
using System.Linq;
using System.Collections.Generic;
public class Test
{
public static void Main()
{
string[] str = {"Two","Six","Three","four","five","One"};
IEnumerable<string> i = str.Where(s => s[0] == 'T');
foreach (string a in i)
{
Console.WriteLine(a);
}
Console.ReadLine();
}
}
ผลลัพธ์จะได้
Two
Three
คำอธิบายในตัวอย่างนี้ได้มีเงื่อนไขคือ where(s=>s[0] =='T') หมายถึง ให้เลือกสมาชิกที่ขึ้นต้นด้วยตัวอักษร T ซึ่งในสมาชิกของตัวแปร str จะมีสมาชิกที่ขึ้นต้นด้วยตัว T มี สองสมาชิกคือ Two,Three
การใช้งาน Group
using System;
using System.Linq;
using System.Collections.Generic;
public class Test
{
public static void Main()
{
string[] str = { "Ant", "Hourse", "Zebra", "Bird", "Dog" };
var q = from s in str orderby s group s by s.Length into mygroups orderby mygroups.Key select new {Animal = mygroups };
foreach (var a in q)
{
foreach (string s in a.Animal)
{
Console.WriteLine(s);
}
}
Console.ReadLine();
}
}
การใช้ SelectMany
using System;
using System.Linq;
using System.Collections.Generic;
public class Test
{
public static void Main()
{
string[] str = { "1 A B C","2 D E F","3 G H T"};
var q = str.SelectMany(s => s.Split(' '));
foreach (string a in q)
{
Console.WriteLine(a);
}
Console.ReadLine();
}
}
ตัวอย่างการแสดงชนิดของสมาชิกใน array
using System;
using System.Linq;
public class TestLamda
{
public static void Main()
{
object[] obj = { 1, "A", 2, "B", 3, "C" };
var q = obj.Select(o => o.GetType().Name).OrderBy(o => o);
foreach (object o in q)
{
Console.WriteLine(o);
}
Console.ReadLine();
}
}
LINQ to XML
ตัวอย่าง
using System;
using System.Linq;
using System.Xml.Linq;
public class TestLINQ
{
public static void Main()
{
var animals = new[] { new { Name = "puppy", Color = "Brown" }, new { Name = "doppy", Color = "White" }, new { Name = "bamby", Color = "Black" } };
XElement x = new XElement("animals", from a in animals select new XElement("animal", new XAttribute("Name", a.Name), new XAttribute("Color", a.Color)));
Console.WriteLine(x);
Console.ReadLine();
}
}