返回Linq的记录列表

时间:2011-03-11 08:10:24

标签: c# linq

我有List<Customer>Customer,客户有字段:Id,FirstName,LastName

我有List<Records>Records,记录有字段:CustomerId,RecordId

我有List<Record> Record,记录有字段:Id,FieldA,FieldB

我希望取回所有记录,具体取决于List<Customer>表示客户列表中客户的所有记录

你有什么想法吗?

谢谢,

2 个答案:

答案 0 :(得分:3)

我认为这次加入会有效:

from c in Customers
join r1 in Records on c.Id equals r1.CustomerId
join r2 in Record on r1.RecordId equals r2.Id
select r2

但我也认为“记录”可能更好地命名为CustomerRecordLink或类似的

答案 1 :(得分:0)

一个简单的人会使用连接吗?

List<Record> result =
    (from c in Customer
    from rs in Records
    from r in Record
    where
    rs.CustomerId == c.id
    && r.id == rs.RecordId
    select r).Distinct().ToList();
相关问题