1

我希望有人可以帮助我解决以下问题。

我的桌子就像,

(Job)
JobID
CustomerID
JobPartID

Job -> JobPart (One to Many)
(JobPart)
JobPartID
JobID [Foreign Key – ref JobID(JobID)]
Pickup-AddressID [Foreign Key – ref Address(AddressID)]
Destination-AddressID [Foreign Key – ref Address(AddressID)]

Address is a separate table
(Address)
AddressID
HouseNo
Street
City
Etc..

我想要的是,对于特定客户的所有 JobPart 以及地址详细信息。

4

1 回答 1

0

好吧,它甚至不会编译为有效的 c#,但我认为你正在寻找的是这样的:

var results = 
    from j in job
    from jp in j.JobParts
    where j.CustomerID == 1
    select new { jp.PickupAddress, jp.DestinationAddress };

假设您在JobParts和之间设置了正确的导航属性,这将起作用Addresses

你可以很容易地使用它:

foreach(var record in results)
{
    var pickupHouseNo = record.PickupAddress.HouseNo;
    var pickupStreet = record.PickupAddress.Street;
    // ...
    var destinationHouseNo = record.DestinationAddress.HouseNo;
    var destinationStreet = record.DestinationAddress.Street;
    // do stuff
}

如果您真的希望它们作为平面查询结果,只需使用以下命令:

var results = 
    from j in job
    from jp in j.JobParts
    where j.CustomerID == 1
    select new 
    {
        PickupHouseNo = jp.PickupAddress.HouseNo, 
        PickupStreet = jp.PickupAddress.Street, 
        // ...
        DestinationHouseNo = jp.DestinationAddress.HouseNo , 
        DestinationStreet = jp.DestinationAddress.Street, 
    };
于 2013-06-04T00:54:02.153 回答