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 > WHERE IN / NOT IN (LINQ, Entity Framework)



Clound SSD Virtual Server

WHERE IN / NOT IN (LINQ, Entity Framework)

WHERE IN / NOT IN (LINQ, Entity Framework) ในหัวข้อนี้เราจะมารู้วิธีการใช้ WHERE IN และ NOT IN บน LINQ to Entities ซึ่งโดยปกติแล้วคำสั่งนี้เราสามารถใช้ OR แทนได้ แต่จะไม่นิยมเพราะการทำงานจะช้ากกว่า IN และ IN ยังสามารถประยุกต์ใช้ได้กับชุดข้อความที่เป็น Array และยังสามารถเอาค่า Result จาก LINQ ชุดที่ 1 ไปใช้ในยัง LINQ ชุดอื่น ๆ ได้อีกด้วย โดย Syntax ของ LINQ ที่เราใช้จะใช้ Contains

Example 1 : การใช้ Where In ด้วยค่าที่มาจาก Array

Code (C#)
        private void frmMain_Load(object sender, EventArgs e)
        {
            // Create new entities Object
            using (var db = new myDatabaseEntities())
            {
                // Array Country Code
                string[] arrCountryCode = new string[] { "TH","US" };

                // Get data from CUSTOMER
                var ds = (from c in db.CUSTOMER
                          where arrCountryCode.Contains(c.COUNTRY_CODE)
                          select c).ToList();

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


Code (VB.Net)
    Private Sub frmMain_Load(sender As Object, e As EventArgs) Handles MyBase.Load
        ' Create new entities Object
        Using db = New myDatabaseEntities()
            ' Array Country Code
            Dim arrCountryCode As String() = New String() {"TH", "US"}

            ' Get data from CUSTOMER
            Dim ds = (From c In db.CUSTOMER Where arrCountryCode.Contains(c.COUNTRY_CODE)
                   Select c).ToList()

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

Screenshot

WHERE IN / NOT IN LINQ, Entity Framework



Example 2 : การ Where In ของ Result จาก LINQ ที่หนึ่งไป LINQ ที่สอง

Code (C#)
        private void frmMain_Load(object sender, EventArgs e)
        {
            // Create new entities Object
            using (var db = new myDatabaseEntities())
            {

                // Get COUNTRY
                var country = (from c in db.COUNTRY
                               where c.COUNTRY_CODE == "US" || c.COUNTRY_CODE == "TH" 
                               select c.COUNTRY_CODE).Distinct();

                // Get data from CUSTOMER
                var customer = (from c in db.CUSTOMER
                                where country.Contains(c.COUNTRY_CODE)
                          select c).ToList();

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

            }
        }
Code (VB.Net)
    Private Sub frmMain_Load(sender As Object, e As EventArgs) Handles MyBase.Load
        ' Create new entities Object
        Using db = New myDatabaseEntities()

            ' Get COUNTRY
            Dim country = (From c In db.COUNTRY
                           Where c.COUNTRY_CODE = "US" Or c.COUNTRY_CODE = "TH"
                           Select c.COUNTRY_CODE).Distinct()

            ' Get data from CUSTOMER
            Dim customer = (From c In db.CUSTOMER
                            Where country.Contains(c.COUNTRY_CODE)
                            Select c).ToList()

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

Screenshot

WHERE IN / NOT IN LINQ, Entity Framework

ในกรณีที่ต้องการ NOT IN ให้ใช้เครื่องหมาย !








Code (C#)
where !country.Contains(c.COUNTRY_CODE)
Code (VB.net)
where Not country.Contains(c.COUNTRY_CODE)

Screenshot

WHERE IN / NOT IN LINQ, Entity Framework

   
Share


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


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


   


Bookmark.   
       
  By : ThaiCreate.Com Team (บทความเป็นลิขสิทธิ์ของเว็บไทยครีเอทห้ามนำเผยแพร่ ณ เว็บไซต์อื่น ๆ)
  Score Rating :  
  Create/Update Date : 2015-10-02 21:15:02 / 2017-03-24 23:05:51
  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 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 อัตราราคา คลิกที่นี่