3 回答
TA贡献1906条经验 获得超3个赞
请注意,TransationList按BatchId升序排序,然后按CardId降序排序。
var TransactionList = context.Trx
.GroupBy(t => t.BatchId) //group by BatchId
.OrderBy(t => t.Key) //order by BatchId
.Select
(g =>
//Create a new "Batch Group" anonymously where each batch contains the associated transactions
new
{
BatchId = g.Key,
BatchTransactions = g.Select(trx => new { Card = trx.Card, User = trx.User }).OrderByDescending(batchTrx => batchTrx.Card.CardId), //order by CardId
}
);
要遍历排序列表,可以使用嵌套的foreach循环,如下所示:
//Loop through the batch group in the sorted list
foreach(var batchGroup in TransactionList)
{
foreach(var batchTrx in batchGroup.BatchTransactions)
{
//You may access the properties of batchTrx like a normal object graph, example batchTrx.Card.CardId
}
}
TA贡献1809条经验 获得超8个赞
你可以SelectMany
实际使用
var transactionList = db.Transactions.GroupBy(x => new { x.BatchID }).SelectMany(x => x).ToList();
TA贡献1719条经验 获得超6个赞
// doing Select(x => x.ToList()) throws away group key!
var TransactionList = db.Transactions.GroupBy(x => x.BatchID).ToList();
foreach (var group in TransactionList)
{
// now group variable is a ... well, group of transactions. :)
// you can get its key and iterate over sub-collection of transactions in this group.
Response.Write($"Group {group.Key}:\n");
foreach (var item in group)
{
Response.Write($" Transaction {item.TTransactionID}\n");
}
}
- 3 回答
- 0 关注
- 820 浏览
添加回答
举报