在提出更好的替代方案之前,您可以使用以下方法。
var q = db.Recipients;
// this loads all table rows...
// so you should filter q as per
// needed rowset only
var listq = q
.SelectMany(x =>
x.Table.Select( (y,i)=> new {
Key = y.RecipientID + "-" + ((int)i/(int)40),
Value = y
})
);
var model = listq.GroupBy(x=>x.Key)
.Select( x=> new {
Recipient = x.Select(y=>y.Value.Recipient).First(),
Table = x.Select(y=>y.Value)
});
- 在第一步中,我们将从数据库中获取所有具有表格导航属性的记录。
- 接下来,我们将调用 SelectMany 来展平层次结构,但是,我们将创建一个新的 Key,其中包含 RecipientID 和除以 40 的索引,因此第一个 Recipient 为 1,索引为 1,因此 Key 将为 1-0,依此类推对于第 41 个索引,Key 将为 1-1。
- 然后我们将按新创建的 Key 进行分组,
- 然后我们将通过选择第一个收件人和枚举表中的其余部分来选择收件人和表。
完整的工作样本在这里..
public class Recipient
{
public Recipient()
{
Table = new List<Table>();
}
public long RecipientID { get; set; }
public List<Table> Table { get; set; }
}
public class Table
{
public long TableID { get; set; }
public long RecipientID { get; set; }
public Recipient Recipient { get; set; }
}
private static void Add(List<Recipient> list, long index, int p2)
{
Recipient r = new Recipient
{
RecipientID = index
};
for (int i = 0; i < p2; i++)
{
Table t = new Table
{
TableID = i,
Recipient = r,
RecipientID = r.RecipientID
};
r.Table.Add(t);
}
list.Add(r);
}
static void Main(string[] args)
{
List<Recipient> list = new List<Recipient>();
Add(list, 1, 80);
Add(list, 2, 15);
Add(list, 3, 99);
var listq = list
.SelectMany(x =>
x.Table.Select( (y,i)=> new {
Key = y.RecipientID + "-" + ((int)i/(int)40),
Value = y
})
);
var model = listq.GroupBy(x=>x.Key)
.Select( x=> new {
Recipient = x.Select(y=>y.Value.Recipient).First(),
Table = x.Select(y=>y.Value)
});
foreach (var item in model)
{
Console.WriteLine("{0}={1}",item.Recipient.RecipientID,item.Table.Count());
}
Console.ReadLine();
}