locked
Linq join with if statement RRS feed

  • Question

  • User1139353921 posted

    how to join two table in if statement using linq query

    Check if a table column value exists in another table column using linq if statement

    Wednesday, July 22, 2015 2:34 AM

Answers

  • User-271186128 posted

    Hi Manikandan Ulagu,

    i need  if record exists or not  using join..

    if(here need to join two tow table)

    From your description, I suppose you want to check whether the two tables contains the same values. If that is the case, you could refer to the following code:

                    var query = context.Table2;
                    GridView1.DataSource = query.ToList();
                    GridView1.DataBind();
    
                    var query2 = context.Table4;
                    GridView2.DataSource = query2.ToList();
                    GridView2.DataBind();
    
                    var query3 = (from cc in context.Table2
                                 join tt in context.Table4 on cc.ID equals tt.ID
                                 where cc.Value == tt.Name
                                 select cc.ID).Count();
                    if (query3 > 0)
                    {
                        Response.Write("Table2 and Table4 contains the same Values.");
                    }
                    else
                    {
                        Response.Write("Table2 and Table4 doesn't contains the same Values.");
                    }

    The output screenshot:

    Best Regards,
    Dillion

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Thursday, July 23, 2015 2:46 AM

All replies

  • User2024324573 posted

    Assuming you have two tables Customers and Orders, and each Customer is mapped with Order by CustomerID, so to get the list of orders for a particular customer you can write the join query as below:

    var custOrders = from c in Customers
    join o in Orders on c.CustomerId equals o.CustomerId
    where c.Customers.City ="CA"
    select c;

    Hope this will help

    Wednesday, July 22, 2015 2:49 AM
  • User1139353921 posted

    Hi

    i need  if record exists or not  using join..

    if(here need to join two tow table)

    {

    }

    Wednesday, July 22, 2015 3:03 AM
  • User-271186128 posted

    Hi Manikandan Ulagu,

    i need  if record exists or not  using join..

    if(here need to join two tow table)

    From your description, I suppose you want to check whether the two tables contains the same values. If that is the case, you could refer to the following code:

                    var query = context.Table2;
                    GridView1.DataSource = query.ToList();
                    GridView1.DataBind();
    
                    var query2 = context.Table4;
                    GridView2.DataSource = query2.ToList();
                    GridView2.DataBind();
    
                    var query3 = (from cc in context.Table2
                                 join tt in context.Table4 on cc.ID equals tt.ID
                                 where cc.Value == tt.Name
                                 select cc.ID).Count();
                    if (query3 > 0)
                    {
                        Response.Write("Table2 and Table4 contains the same Values.");
                    }
                    else
                    {
                        Response.Write("Table2 and Table4 doesn't contains the same Values.");
                    }

    The output screenshot:

    Best Regards,
    Dillion

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Thursday, July 23, 2015 2:46 AM