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 


No comments:

Post a Comment

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 ...