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 > Select Entity to List (LINQ, Entity Framework)



Clound SSD Virtual Server

Select Entity to List (LINQ, Entity Framework)

Select Entity to List<Object> (LINQ, Entity Framework) ปกติแล้วในการ Select ข้อมูลจาก EntitySet แล้วใช้ .ToList() เราจะได้โครงสร้าง List ที่เป็น Object ที่มี Attribute ตามโครงสร้างของ Table แต่ถ้าต้องการให้เป็น Object ตามโคางสร้างก็สามารถสร้าง Class ขึ้นมาแล้วตอนที่ทำการเลือก EntitySet ก็ใช้การ New ให้เป็น List ตามที่โครงสร้างตาม Object ที่เราประกาศขึ้น

Example การ Select ข้อมูลจาก EntitySet แล้วแปลงค่าให้เป็น List Object ใหม่

Select Entity to List

รายการข้อมูลใน Table

Code (C#)
        public class CustomerInfo
        {
            public string CusID { get; set; }
            public string CusName { get; set; }
            public string CountryName { get; set; }
        }

        private void frmMain_Load(object sender, EventArgs e)
        {
            // Create new entities from Entities
            using (var db = new myDatabaseEntities())
            {
                // Create List CustomerInfo Object
                List<CustomerInfo> ls;

                // Get data from CUSTOMER and join COUNTRY
                ls = (from c in db.CUSTOMER
                          join d in db.COUNTRY on c.COUNTRY_CODE equals d.COUNTRY_CODE
                          select new CustomerInfo
                          {
                              CusID = c.CUSTOMER_ID,
                              CusName = c.NAME,
                              CountryName = d.COUNTRY_NAME
                          }).ToList();

                // if found item rows
                if (ls.Count() > 0)
                {
                    this.myDataGridView.DataSource = ls;
                }

            }
        }


Code (VB.Net)
Public Class CustomerInfo
	Public Property CusID() As String
		Get
			Return m_CusID
		End Get
		Set
			m_CusID = Value
		End Set
	End Property
	Private m_CusID As String
	Public Property CusName() As String
		Get
			Return m_CusName
		End Get
		Set
			m_CusName = Value
		End Set
	End Property
	Private m_CusName As String
	Public Property CountryName() As String
		Get
			Return m_CountryName
		End Get
		Set
			m_CountryName = Value
		End Set
	End Property
	Private m_CountryName As String
End Class

Private Sub frmMain_Load(sender As Object, e As EventArgs)
	' Create new entities from Entities
	Using db = New myDatabaseEntities()
		' Create List CustomerInfo Object
		Dim ls As List(Of CustomerInfo)

		' Get data from CUSTOMER and join COUNTRY
		ls = (From c In db.CUSTOMER
			Join d In db.COUNTRY On c.COUNTRY_CODE = d.COUNTRY_CODE 
			New CustomerInfo()  With { _
			.CusID = c.CUSTOMER_ID, _
			.CusName = c.NAME, _
			.CountryName = d.COUNTRY_NAME _
		}).ToList()

		' if found item rows
		If ls.Count() > 0 Then
			Me.myDataGridView.DataSource = ls
		End If
	End Using
End Sub

Screenshot

Select Entity to List

Result ที่ได้








   
Share


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


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


   


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

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

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

 
List(T) คืออะไร Generic Class ของ List ใช้งานกับ Entity Framework และ LINQ to Entities
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 :

 
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 อัตราราคา คลิกที่นี่