Showing posts with label LINQ. Show all posts
Showing posts with label LINQ. Show all posts

Wednesday, July 2, 2014

How to do INNER JOIN, LEFT JOIN and RIGHT JOIN in LINQ as well as in SQL? Part 2

Before you see this VB.Net LINQ Query, Please go through Part 1.

Now we will write LINQ Queries in VB.Net for INNER JOIN, LEFT JOIN and RIGHT JOIN.

            
       Using db As New MyTestEntities

            'Inner Join '
            Dim results = (From cust As Customer In db.Customers _
                           Join ct As City In db.Cities _
                           On ct.Id Equals cust.City_Id _
                           Select cust.Name).ToList()

            'Left Join '
            results = (From cust As Customer In db.Customers _
                           Group Join ct As City In db.Cities _
                           On ct.Id Equals cust.City_Id Into Joined = Group _
                           From lj In Joined.DefaultIfEmpty() _
                           Select cust.Name).ToList()

            'Right Join (Just swap it)  '
            results = (From ct As City In db.Cities _
                           Group Join cust As Customer In db.Customers _
                           On ct.Id Equals cust.City_Id Into Joined = Group _
                           From rj In Joined.DefaultIfEmpty() _
                           Select ct.Name).ToList()

        End Using 


Wednesday, June 25, 2014

How to do INNER JOIN, LEFT JOIN and RIGHT JOIN in LINQ as well as in SQL? Part 1

Today I will show you how to write LINQ Query as well as SQL Query. Both LINQ and SQL examples will clear your idea about INNER JOIN, LEFT JOIN and RIGHT JOIN. And also it will show you different results based on different JOINs.

Let’s create platform for it. Suppose we have following tables with data.
SELECT * FROM CUSTOMER


SELECT * FROM CITY


Now we will write SQL Queries for INNER JOIN, LEFT JOIN and RIGHT JOIN.
--Inner Join
SELECT cust.Name 
FROM CUSTOMER cust
 INNER JOIN CITY ct 
  ON ct.id = cust.city_id
  
--Left Join
SELECT cust.Name
FROM CUSTOMER cust
 LEFT JOIN CITY ct 
  ON ct.id = cust.city_id
 
--Right Join
SELECT ct.name AS City  
FROM CUSTOMER cust
 RIGHT JOIN CITY ct 
  ON ct.id = cust.city_id

After SQL, we will write LINQ Queries in C# for INNER JOIN, LEFT JOIN and RIGHT JOIN.
            
using (MyTestEntities db = new MyTestEntities())
{

 //Inner Join
 var results = (from Customer cust in db.Customers
                join City ct in db.Cities
                on cust.City_Id equals ct.Id
                select cust.Name).ToList();


 //Left Join
 results = (from Customer cust in db.Customers
            join City ct in db.Cities 
            on cust.City_Id equals ct.Id into Joined
            from lj in Joined.DefaultIfEmpty()
            select cust.Name).ToList();


 //Right Join (Just swap it)
 results = (from City ct in db.Cities
            join Customer cust in db.Customers 
            on ct.Id equals cust.City_Id into Joined
            from rj in Joined.DefaultIfEmpty()
            select ct.Name).ToList();

}
Now it's time to see results:

INNER JOIN Result for both queries will be like this



LEFT JOIN Result for both queries will be like this



RIGHT JOIN Result for both queries will be like this




Now you can compare SQL, LINQ and Result to get clear idea about it.

Wednesday, June 11, 2014

What is LINQ (Language-Integrated Query)?

Language-Integrated Query (LINQ) is a set of features introduced in Visual Studio 2008 that extends powerful query capabilities to the language syntax of C# and Visual Basic. LINQ introduces standard, easily-learned patterns for querying and updating data, and the technology can be extended to support potentially any kind of data store. Visual Studio includes LINQ provider assemblies that enable the use of LINQ with .NET Framework collections, SQL Server databases, ADO.NET Datasets, and XML documents.

Simple example on LINQ:

All LINQ query operations consist of three distinct actions:

1.Obtain the data source.
2.Create the query.
3.Execute the query.

    Public Class Customer
        Public Property Name As String
        Public Property City As String
    End Class
        'The Three Parts of a LINQ Query '

        '1. Data source '
        Dim customers As New List(Of Customer)

        Dim objCustomer As New Customer
        objCustomer.Name = "Miller"
        objCustomer.City = "Camp Hill"
        customers.Add(objCustomer)

        objCustomer = New Customer
        objCustomer.Name = "John"
        objCustomer.City = "Harrisburg"
        customers.Add(objCustomer)

        objCustomer = New Customer
        objCustomer.Name = "Wayne"
        objCustomer.City = "Enola"
        customers.Add(objCustomer)

        objCustomer = New Customer
        objCustomer.Name = "Julie"
        objCustomer.City = "Enola"
        customers.Add(objCustomer)

        '2. Query creation '
        Dim enolaCustomers = From oCustomer As Customer In customers _
                               Where oCustomer.City = "Enola" _
                               Select oCustomer


        '3. Query Execution '
        For Each oCustomer As Customer In enolaCustomers
            Response.Write(oCustomer.Name + "<br/>")
        Next
        'Result will be :'
        Wayne
        Julie

React-select is very slow on larger list - Found solution - using react-window

 I had more than 4000 items in searchable dropdownlist. I have used react-select but it was very slow. finally I found complete solution to ...