Register Register Member Login Member Login Member Login Forgot Password ??
PHP , ASP , ASP.NET, VB.NET, C#, Java , jQuery , Android , iOS , Windows Phone
 

Registered : 109,027

HOME > Entity Framework (EF) สอนเขียน LINQ to Entities บน .NET Application > List(T) คืออะไร Generic Class ของ List ใช้งานกับ Entity Framework และ LINQ to Entities



Clound SSD Virtual Server

List(T) คืออะไร Generic Class ของ List ใช้งานกับ Entity Framework และ LINQ to Entities

List(T) คืออะไร Generic Class ของ List ใช้งานกับ Entity Framework และ LINQ to Entities เป็น Generic Type ของ List(T) ที่ใช้สำหรับการจัดการ Collection วัตถุมีรูปแบบเหมือนกับ Array แต่เก็บหลายมิติ ตามชนิดและ Data Type ของ Object ที่ต้องการเก็บ แต่ List เป็นมากกว่ากว่า Array คือ List สามารถ จัดการ Index และเข้าถึง Element ของ Object การเพิ่ม-ลบ สมาชิก อ้างถึง Attribute ของ Object การจัดเรียง การสร้าง Where Clause สร้าเงื่อนไข เพื่อค้นหา Object และความสามารถอื่น ๆ ที่สามารถจัดการกับ Element ภายใน Object ได้อย่างมีประสิทธิภาพ

ในการใช้งาน List(T) จะต้องทำการ Import หรือ using คลาสของ System.Collections.Generic ก่อนที่จะเรียกใช้งาน

Example 1 การสร้าง List(T) แบบง่าย ๆ เก็บค่า Object ที่ Data Type เป็นแบบ String

Code (C#)
static void Main(string[] args)
{
    // Create List
    var ls = new List<string>(new string[] { "String 1", "String 2", "String 3", "String 4", "String 5" });

    // Loop
    foreach (var item in ls)
    {
	Console.WriteLine(item);
    }
}


Code (VB.Net)
Private Sub Main(args As String())
	' Create List
	Dim ls = New List(Of String)(New String() {"String 1", "String 2", "String 3", "String 4", "String 5"})

	' Loop
	For Each item As var In ls
		Console.WriteLine(item)
	Next
End Sub

Result

List(T)  Generic Class  List

เขียนได้อีกวิธีคือ

Code (C#)
static void Main(string[] args)
{
    // Create List
    var ls = new List<string>();

    // Add Item
    ls.Add("String 1");
    ls.Add("String 2");
    ls.Add("String 3");
    ls.Add("String 4");
    ls.Add("String 5");

    // Loop
    foreach (var item in ls)
    {
	Console.WriteLine(item);
    }
}
Code (VB.Net)
Private Sub Main(args As String())
	' Create List
	Dim ls = New List(Of String)()

	' Add Item
	ls.Add("String 1")
	ls.Add("String 2")
	ls.Add("String 3")
	ls.Add("String 4")
	ls.Add("String 5")

	' Loop
	For Each item In ls
	    Console.WriteLine(item)
	Next
End Sub



Example 2 การสร้าง List(T) แบบง่าย ๆ เก็บค่า Object ที่ Data Type เป็นแบบ Integer (Number)

Code (C#)
static void Main(string[] args)
{
    // Create List
    var ls = new List<int>();

    // Add Item
    ls.Add(10);
    ls.Add(20);
    ls.Add(30);
    ls.Add(40);
    ls.Add(50);

    // Loop
    foreach (var item in ls)
    {
	Console.WriteLine(item);
    }
}
Code (VB.Net)
Private Sub Main(args As String())
	' Create List
	Dim ls = New List(Of Integer)()

	' Add Item
	ls.Add(10)
	ls.Add(20)
	ls.Add(30)
	ls.Add(40)
	ls.Add(50)

	' Loop
	For Each item In ls
		Console.WriteLine(item)
	Next
End Sub

Result

List(T)  Generic Class  List








Example 3 การสร้าง List() กับ Count รายการ Item และการอ้างถึง Item ด้วย Index

Code (C#)
static void Main(string[] args)
{
    // Create List
    var ls = new List<string>();

    // Add Item
    ls.Add("String 1");
    ls.Add("String 2");
    ls.Add("String 3");
    ls.Add("String 4");
    ls.Add("String 5");

    // Count
    Console.WriteLine(string.Format("List count = {0}", ls.Count()));

    // Get by Index
    Console.WriteLine(string.Format("Item index 2 = {0}", ls[2]));
}
Code (VB.Net)
Private Sub Main(args As String())
	' Create List
	Dim ls = New List(Of String)()

	' Add Item
	ls.Add("String 1")
	ls.Add("String 2")
	ls.Add("String 3")
	ls.Add("String 4")
	ls.Add("String 5")

	' Count
	Console.WriteLine(String.Format("List count = {0}", ls.Count()))

	' Get by Index
	Console.WriteLine(String.Format("Item index 2 = {0}", ls(2)))
End Sub

Result

List(T)  Generic Class  List


Example 4 การสร้าง List(T) โดยสร้าง Object จาก Class และ Property

Code (C#)
public class NameInfo
{
    public string NAME { get; set; }
    public string GENER { get; set; }
    public int AGE { get; set; }
}

static void Main(string[] args)
{
    // Create List
    var ls = new List<NameInfo>();

    // Add Item
    ls.Add(new NameInfo { NAME = "Weerachai Nukitram", GENER = "Male", AGE = 34 });
    ls.Add(new NameInfo { NAME = "Wisarut Nukitram", GENER = "Male", AGE = 40 });
    ls.Add(new NameInfo { NAME = "Wipa Nukitram", GENER = "Female", AGE = 18 });

    // Loop
    foreach (var item in ls)
    {
	Console.WriteLine(item.NAME + ", " + item.GENER + ", " + item.AGE);
	//Console.WriteLine(string.Format("{0}, {1}, {2}", item.NAME, item.GENER, item.AGE));
    }

}
Code (VB.Net)
Public Class NameInfo
	Public Property NAME() As String
		Get
			Return m_NAME
		End Get
		Set
			m_NAME = Value
		End Set
	End Property
	Private m_NAME As String
	Public Property GENER() As String
		Get
			Return m_GENER
		End Get
		Set
			m_GENER = Value
		End Set
	End Property
	Private m_GENER As String
	Public Property AGE() As Integer
		Get
			Return m_AGE
		End Get
		Set
			m_AGE = Value
		End Set
	End Property
	Private m_AGE As Integer
End Class

Private Sub Main(args As String())
	' Create List
	Dim ls = New List(Of NameInfo)()

	' Add Item
	ls.Add(New NameInfo() With { _
		.NAME = "Weerachai Nukitram", _
		.GENER = "Male", _
		.AGE = 34 _
	})
	ls.Add(New NameInfo() With { _
		.NAME = "Wisarut Nukitram", _
		.GENER = "Male", _
		.AGE = 40 _
	})
	ls.Add(New NameInfo() With { _
		.NAME = "Wipa Nukitram", _
		.GENER = "Female", _
		.AGE = 18 _
	})

	' Loop
	For Each item In ls
			'Console.WriteLine(string.Format("{0}, {1}, {2}", item.NAME, item.GENER, item.AGE));
		Console.WriteLine(item.NAME + ", " + item.GENER + ", " + item.AGE)
	Next
End Sub

Result

List(T)  Generic Class  List


Example 5 การสร้าง List(T) และตัวอย่างการใช้ Where Clause เพื่อค้นหารายการใน List(T)

Code (C#)
static void Main(string[] args)
{
    // Create List
    var ls = new List<NameInfo>();

    // Add Item
    ls.Add(new NameInfo { NAME = "Weerachai Nukitram", GENER = "Male", AGE = 34 });
    ls.Add(new NameInfo { NAME = "Wisarut Nukitram", GENER = "Male", AGE = 40 });
    ls.Add(new NameInfo { NAME = "Wipa Nukitram", GENER = "Female", AGE = 18 });

    // Where statement
    ls = ls.Where(o => (o.AGE <= 30)).ToList();

    // Loop
    foreach (var item in ls)
    {
	Console.WriteLine(item.NAME + ", " + item.GENER + ", " + item.AGE);
	//Console.WriteLine(string.Format("{0}, {1}, {2}", item.NAME, item.GENER, item.AGE));
    }

}
Code (VB.Net)
Private Shared Sub Main(args As String())
	' Create List
	Dim ls = New List(Of NameInfo)()

	' Add Item
	ls.Add(New NameInfo() With { _
		.NAME = "Weerachai Nukitram", _
		.GENER = "Male", _
		.AGE = 34 _
	})
	ls.Add(New NameInfo() With { _
		.NAME = "Wisarut Nukitram", _
		.GENER = "Male", _
		.AGE = 40 _
	})
	ls.Add(New NameInfo() With { _
		.NAME = "Wipa Nukitram", _
		.GENER = "Female", _
		.AGE = 18 _
	})

	' Where statement
	ls = ls.Where(Function(o) (o.AGE <= 30)).ToList()

	' Loop
	For Each item In ls
			'Console.WriteLine(string.Format("{0}, {1}, {2}", item.NAME, item.GENER, item.AGE));
		Console.WriteLine(item.NAME + ", " + item.GENER + ", " + item.AGE)
	Next
End Sub

Result

List(T)  Generic Class  List








Example 6 การสร้าง List(T) และตัวอย่างการใช้ SUM ค่าใน List ด้วย Syntax ของ LINQ

Code (C#)
static void Main(string[] args)
{
    // Create List
    var ls = new List<NameInfo>();

    // Add Item
    ls.Add(new NameInfo { NAME = "Weerachai Nukitram", GENER = "Male", AGE = 34 });
    ls.Add(new NameInfo { NAME = "Wisarut Nukitram", GENER = "Male", AGE = 40 });
    ls.Add(new NameInfo { NAME = "Wipa Nukitram", GENER = "Female", AGE = 18 });

    // Sum AGE
    int sumAge = ls.Sum(o => (o.AGE));
    Console.WriteLine(string.Format("Sum Age = {0}", sumAge));

    Console.WriteLine("==========================");

    // Sum GROUP BY GENDER
    var lsSumByGender = ls.GroupBy(o => (o.GENER))
		    .Select(
			g => new
			{
			    Gener = g.Key,
			    Age = g.Sum(s => s.AGE),
			}).ToList();
    foreach (var item in lsSumByGender)
    {
	Console.WriteLine(item.Gener + " = " + item.Age);
    }

}
Code (VB.Net)
Private Shared Sub Main(args As String())
	' Create List
	Dim ls = New List(Of NameInfo)()

	' Add Item
	ls.Add(New NameInfo() With { _
		.NAME = "Weerachai Nukitram", _
		.GENER = "Male", _
		.AGE = 34 _
	})
	ls.Add(New NameInfo() With { _
		.NAME = "Wisarut Nukitram", _
		.GENER = "Male", _
		.AGE = 40 _
	})
	ls.Add(New NameInfo() With { _
		.NAME = "Wipa Nukitram", _
		.GENER = "Female", _
		.AGE = 18 _
	})

	' Sum AGE
	Dim sumAge As Integer = ls.Sum(Function(o) (o.AGE))
	Console.WriteLine(String.Format("Sum Age = {0}", sumAge))

	Console.WriteLine("==========================")

	' Sum GROUP BY GENDER
	Dim lsSumByGender = ls.GroupBy(Function(o) (o.GENER)).[Select](Function(g) New With { _
		.Gener = g.Key, _
		.Age = g.Sum(Function(s) s.AGE) _
	}).ToList()
	For Each item In lsSumByGender
		Console.WriteLine(item.Gener + " = " + item.Age)
	Next
End Sub

Result

List(T)  Generic Class  List

ในการใช้งาน Entity Framework ด้วย LINQ to Entities เราค่อนจะมีการนำ List(T) มาใช้งานในเกือบทุก ๆ ส่วน ของการเรียกใช้งาน เพราะปกติแล้วในการเขียนด้วย ADO.Net เราจะได้ค่า Result ที่เป็น DataReader , DataSet หรือ DataTable แล้วค่อยนำมันไปใช้งานในส่วนของ DataSource หรือ Loop , ส่งค่าไปในส่วนอื่น ๆ แต่ใน LING to Entities จะไม่มีชุดคำสั่งเหล่านี้ ฉะนั้นค่าที่ได้จาก LINQ to Entities จะนิยมแปลงไปเป็น List(T) แทน เพราะใน LINQ to Entities สนับสนุนการทำงานร่วมกับ List(T) ค่อนข้างจะมีประสิทธิภาพมาก เช่น

LINQ to Entities (C#)
var ds = (from c in db.CUSTOMER select c);
LINQ to Entities (VB.Net)
Dim ds = (From c In db.CUSTOMER Select c)

จาก คำสั่งนี้จะได้ Result เป็นค่า IEnumerable() เหมือนกับ DataReader ของ System.Data ใน ADO.Net โดยค่านี้ยังมีการ iterate กับ data ด้วย momory ที่ถูกจองไว้ จะไม่ค่อยนิยมนำไปใช้งานต่อ แต่ในกรณีที่นำไปใช้ในส่วนอื่น ๆ ของโปรแกรม จะนิยมทำการแปลง
IEnumerable.ToList() ให้เป็น List(T)

LINQ to Entities (C#)
var ds = (from c in db.CUSTOMER select c).ToList();
LINQ to Entities (VB.Net)
Dim ds = (From c In db.CUSTOMER Select c).ToList()

ซึ่งคำสั่งนี้จะมีได้ IEnumerable.ToList() เหมือนกับ DataTable ของ System.Data ใน ADO.Net ซึ่งมีการใช้ .ToList() จะมีการแปลงให้เป็น Object List (T) อัตโนมัติ ประกอบด้วย List(CUSTOMER) เช่น

LINQ to Entities (C#)
public class CUSTOMER
{
	public string CUSTOMER_ID { get; set; }
	public string NAME { get; set; }
	public string EMAIL { get; set; }
	public string COUNTRY_CODE { get; set; }
	public decimal BUDGET { get; set; }
	public decimal USED { get; set; }
}

List<CUSTOMER> ds = new List<CUSTOMER>();
LINQ to Entities (VB.Net)
Public Class CUSTOMER
	Public Property CUSTOMER_ID() As String
		Get
			Return m_CUSTOMER_ID
		End Get
		Set
			m_CUSTOMER_ID = Value
		End Set
	End Property
	Private m_CUSTOMER_ID As String
	Public Property NAME() As String
		Get
			Return m_NAME
		End Get
		Set
			m_NAME = Value
		End Set
	End Property
	Private m_NAME As String
	Public Property EMAIL() As String
		Get
			Return m_EMAIL
		End Get
		Set
			m_EMAIL = Value
		End Set
	End Property
	Private m_EMAIL As String
	Public Property COUNTRY_CODE() As String
		Get
			Return m_COUNTRY_CODE
		End Get
		Set
			m_COUNTRY_CODE = Value
		End Set
	End Property
	Private m_COUNTRY_CODE As String
	Public Property BUDGET() As Decimal
		Get
			Return m_BUDGET
		End Get
		Set
			m_BUDGET = Value
		End Set
	End Property
	Private m_BUDGET As Decimal
	Public Property USED() As Decimal
		Get
			Return m_USED
		End Get
		Set
			m_USED = Value
		End Set
	End Property
	Private m_USED As Decimal
End Class

Dim ds As New List(Of CUSTOMER)()

ซึ่ง Object ต่าง ๆ เหล่านี้ จะมีการแปลงมาให้อัตโนมัติ หลังจากที่ใช้ IEnumerable.ToList()



ส่วนในเรื่องของ Performance ของ List(T) หลายคนอาจจะกังวลว่า ในกรณีที่มีการ Query ข้อมูลเป็นจำนวนมากแล้วจับลง List(T) จะมีปัญหาในเรื่องการทำงานช้าหรือใช้ Memory จำนวนมากหรือไม่ ในความคิดเห็นส่วนตัวและอ่านจากหลาย ๆ ความคิดเห็นของกูรูทั้งหลาย การใช้งาน List(T) จะมีผลเรื่อง Performance และ Memory จริง แต่มันเป็นกระบวนการที่น้อยมาก เมื่อเปรียบเทียบกับผลัพธ์ ของกระบวนการนำไปใช้งานจริง ซึ่งสามารถใช้งานได้ไม่มีปัญหา เมื่อข้อมูลมีปริมาณมากแน่นอน

   
Share


ช่วยกันสนับสนุนรักษาเว็บไซต์ความรู้แห่งนี้ไว้ด้วยการสนับสนุน Source Code 2.0 ของทีมงานไทยครีเอท


ลองใช้ค้นหาข้อมูล


   


Bookmark.   
       
  By : ThaiCreate.Com Team (บทความเป็นลิขสิทธิ์ของเว็บไทยครีเอทห้ามนำเผยแพร่ ณ เว็บไซต์อื่น ๆ)
  Score Rating :  
  Create/Update Date : 2015-09-30 09:11:03 / 2017-03-24 22:56:08
  Download : No files
 Sponsored Links / Related

 
รู้จัก Entity Framework (EF) คืออะไร ใช้ทำอะไร และมีประโยชน์อย่างไรบน .Net Framework
Rating :

 
สร้าง Entity Framework เพื่อติดต่อกับ Database การสร้าง Model Entities บน Visual Studio
Rating :

 
เรียกใช้งาน Entity Framework การ Select ข้อมูลจาก Entities แสดงผลบน DataSource Control
Rating :

 
Loop and Get Data (LINQ, Entity Framework)
Rating :

 
WHERE Clause (LINQ, Entity Framework)
Rating :

 
Entity & Relation (Foreign Key , CasCade , Delete , Update) (LINQ, Entity Framework)
Rating :

 
WHERE IN / NOT IN (LINQ, Entity Framework)
Rating :

 
WHERE LIKE / NOT LIKE (LINQ, Entity Framework)
Rating :

 
ORDER BY and Sorting Data (ASC, DESC) (LINQ, Entity Framework)
Rating :

 
WHERE BETWEEN (LINQ, Entity Framework)
Rating :

 
Compare DateTime (LINQ, Entity Framework)
Rating :

 
Join Clause (LINQ, Entity Framework)
Rating :

 
Left Join (LINQ, Entity Framework)
Rating :

 
Right Join (LINQ, Entity Framework)
Rating :

 
Union (LINQ, Entity Framework)
Rating :

 
VIEW Table (LINQ, Entity Framework)
Rating :

 
Add Entity : Insert Rows Into Database (LINQ, Entity Framework)
Rating :

 
Update Entity : Update Rows in Database (LINQ, Entity Framework)
Rating :

 
Delete Entity : Delete Rows in Database (LINQ, Entity Framework)
Rating :

 
Entity Framework and Try Catch Exception (LINQ, Entity Framework)
Rating :

 
Entity Framework and Transaction Scope (LINQ, Entity Framework)
Rating :

 
Get Connection String & Connection State (LINQ, Entity Framework)
Rating :

 
Select Entity to List<Object> (LINQ, Entity Framework)
Rating :

 
Entity Execute SQL Query : INSERT/UPDATE/DELETE (LINQ, Entity Framework)
Rating :

 
Entity Select SQL (Statement) Query (LINQ, Entity Framework)
Rating :

 
Entity and Stored Procedure (LINQ, Entity Framework)
Rating :

 
เทคนิคการใช้ IntelliTrace ในการ Debug ตรวจสอบ Performance ของโปรแกรม
Rating :

 
Windows Form ตอนที่ 1 : DataGridView List Show Data (LINQ, Entity Framework)
Rating :

 
Windows Form ตอนที่ 2 : DataGridView Search Data (LINQ, Entity Framework)
Rating :

 
Windows Form ตอนที่ 3 : DataGridView Display Master-Detail (LINQ, Entity Framework)
Rating :

 
Windows Form ตอนที่ 4 : Add Data (LINQ, Entity Framework)
Rating :

 
Windows Form ตอนที่ 5 : Update Data (LINQ, Entity Framework)
Rating :

 
Windows Form ตอนที่ 6 : Delete Data (LINQ, Entity Framework)
Rating :

 
ASP.Net ตอนที่ 1 : GridView List Show Data (LINQ, Entity Framework)
Rating :

 
ASP.Net ตอนที่ 2 : GridView Search Data (LINQ, Entity Framework)
Rating :

 
ASP.Net ตอนที่ 3 : Display Master-Detail (LINQ, Entity Framework)
Rating :

 
ASP.Net ตอนที่ 4 : Add Data (LINQ, Entity Framework)
Rating :

 
ASP.Net ตอนที่ 5 : Update Data (LINQ, Entity Framework)
Rating :

 
ASP.Net ตอนที่ 6 : Delete Data (LINQ, Entity Framework)
Rating :

 
การใช้งาน Entity Framework กับ MySQL Database (LINQ to Entities - MySQL Database)
Rating :

 
การใช้งาน Entity Framework กับ Oracle Database (LINQ to Entities - Oracle Database)
Rating :


ThaiCreate.Com Forum


Comunity Forum Free Web Script
Jobs Freelance Free Uploads
Free Web Hosting Free Tools

สอน PHP ผ่าน Youtube ฟรี
สอน Android การเขียนโปรแกรม Android
สอน Windows Phone การเขียนโปรแกรม Windows Phone 7 และ 8
สอน iOS การเขียนโปรแกรม iPhone, iPad
สอน Java การเขียนโปรแกรม ภาษา Java
สอน Java GUI การเขียนโปรแกรม ภาษา Java GUI
สอน JSP การเขียนโปรแกรม ภาษา Java
สอน jQuery การเขียนโปรแกรม ภาษา jQuery
สอน .Net การเขียนโปรแกรม ภาษา .Net
Free Tutorial
สอน Google Maps Api
สอน Windows Service
สอน Entity Framework
สอน Android
สอน Java เขียน Java
Java GUI Swing
สอน JSP (Web App)
iOS (iPhone,iPad)
Windows Phone
Windows Azure
Windows Store
Laravel Framework
Yii PHP Framework
สอน jQuery
สอน jQuery กับ Ajax
สอน PHP OOP (Vdo)
Ajax Tutorials
SQL Tutorials
สอน SQL (Part 2)
JavaScript Tutorial
Javascript Tips
VBScript Tutorial
VBScript Validation
Microsoft Access
MySQL Tutorials
-- Stored Procedure
MariaDB Database
SQL Server Tutorial
SQL Server 2005
SQL Server 2008
SQL Server 2012
-- Stored Procedure
Oracle Database
-- Stored Procedure
SVN (Subversion)
แนวทางการทำ SEO
ปรับแต่งเว็บให้โหลดเร็ว


Hit Link
   







Load balance : Server 04
ThaiCreate.Com Logo
© www.ThaiCreate.Com. 2003-2024 All Rights Reserved.
ไทยครีเอทบริการ จัดทำดูแลแก้ไข Web Application ทุกรูปแบบ (PHP, .Net Application, VB.Net, C#)
[Conditions Privacy Statement] ติดต่อโฆษณา 081-987-6107 อัตราราคา คลิกที่นี่