如何在LINQ中执行内连接查询

时间:2013-08-05 09:10:40

标签: c# sql-server linq sql-to-linq-conversion

我有三个表,我需要使用C#为LINQ执行内连接查询。我可以走到一半,但我是初学者,需要帮助,非常感谢

SQL查询

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 个答案:

答案 0 :(得分:5)

我认为这样的事情可以解决问题:

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
    }
)

答案 1 :(得分:2)

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";

答案 2 :(得分:2)

试试这个

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 
}