How to perform inner join Query in LINQ

239 Views Asked by At

I Have three tables and I need to perform inner join query for LINQ using C#. I can go half way but i am very beginner, need help in that many thanks

SQL Query

Select O.CarID, C.customerID, C.Name, C.Address, V.Brand, V.Model, V.Model_Year 
FROM Car_OwnerShip As O, Customers As C, Cars As V
WHERE V.CarID = O.CarID AND O.customerID = C.customerID AND C.Name='toxic';

enter image description here

3

There are 3 best solutions below

5
On

I think that something like this will do the trick:

Car_Ownership
.Join
(
    Customers,
    o=>o.customerID,
    c=>c.customerID,
    (o,c)=>new{o,c}
)
.Join
(
    Cars,
    o=>o.o.CarId,
    v=>v.CarID,
    (o,v)=>new{o,v}
)
.Where
(
    x=>
    x.o.c.Name == "toxic"
)
.Select
(
    x=>
    new
    {
        CarID = x.o.o.CarID,
        customerID = x.o.c.customerID,
        Name = x.o.c.Name,
        Address = x.o.c.Address,
        Brand = x.v.Brand,
        Model = x.v.Model,
        Model_Year = x.v.Model_Year
    }
)
3
On
var query = from owner in Car_OwnerShip
            from customer in Customers
            from car in Cars 
            select new { owner, customer, car }
            where owner.CarID = car.CarID and owner.CustomerID = customer.CustomerId and customer.Name = "toxic";
0
On

Try this

from o in Car_OwnerShip
join c in Customers on o.customerID equals c.customerID
join v in Cars on o.CarID equals v.CarID 
where c.Name=='toxic'
select new {
O.CarID, C.customerID, C.Name, C.Address, V.Brand, V.Model, V.Model_Year 
}