0

我有 4 张桌子:

地址 - AddressID、城市、地址、邮政编码

OBAddress - OBID、AddressID

OB-OBID

状态 - StateID,StateAbbreviation

如何加入多对多关系的三个表?我试过:

var recordsPhysicalAddress = ( from a in db.Addresses
    join oba in db.obAddresses on a.AddressID equals oba.AddressID
    join ob in db.obAddresses on oba.obID equals passedinID
    join s in db.States on a.StateID equals s.StateID
    where oba.obID == ob.obID
    && a.AddressTypeID == '5' //5 is records physical address
    select new
    {
        a.Address1, 
        a.City, 
        a.StateID,
        s.StateAbbreviation,
        a.ZipCode
    }).FirstOrDefault();
4

1 回答 1

1
var query = (from a in db.Addresses
             join oba in db.obAddresses on a.AddressID equals oba.AddressID
             // do not join with db.obAddresses again
             join s in db.States on a.StateID equals s.StateID
             where oba.obID == passedinID // filter here
                   && a.AddressTypeID == '5'
             select new {
                   a.Address,
                   a.City,
                   a.StateID,
                   s.StateAbbreviation,
                   a.ZipCode
             }).FirstOrDefault();

我在评论中添加了一些注释以供查询。但是仍然想知道为什么您提到了第四张桌子?

于 2012-10-23T17:15:12.137 回答