EF 1.0展平左连接查询

时间:2011-07-07 16:47:40

标签: entity-framework linq-to-entities

我正在尝试执行linq以在(Visual Studio 3.5 / EF 1.0)中为3个具有以下关系的实体进行查询:

xcCRMCounterparty * => 0..1 CSIDsInUse
CSIDsInUse 1 => * xcCIFToCSID

以前在T SQL中我用过:

select distinct CIF, xcCIFToCSID.xcCSID, CounterpartyName 
from xcCIFToCSID 
left join CSIDsInUse 
on xcCIFToCSID.idCSID = CSIDsInUse.Id
left join xcCRMCounterparty
on CSIDsInUse.Id = xcCRMCounterparty.IdCSID 
order by CounterpartyName

现在使用EF我试图压缩结果集如下,但我不知道如何压扁xcCRMCounterparty项目,即它是一个集合而不是一个字段

 var query = from cifto in entities.xcCIFToCSIDSet.Include(x => x.CSIDsInUse).Include(x => x.CSIDsInUse.xcCRMCounterparty)
                    select new 
                    { 
                        cifto.CIF,
                        cifto.xcCSID,
                        cifto.CSIDsInUse.xcCRMCounterparty
                    };

如何修改查询以便生成扁平结果?

1 个答案:

答案 0 :(得分:1)

试试这个:

var query = from cifto in entities.xcCIFToCSIDSet
            from x in cifto.CSIDsInUse.xcCRMCouterparty
            select new 
                {
                    cifto.CIF,
                    cifto.xcCSID,
                    x.Name
                };