如何循环遍历System.Collections.Generic.List`1 [CardDistro.Models.Transaction]

时间:2018-05-02 02:48:02

标签: c# linq

我无法从谷歌搜索中得到明确的指示。我有一个名为transaction的表格,我希望按BatchID进行分组,并按descending按外键排序。

表定义

[TTransactionID]      INT          IDENTITY (1, 1) NOT NULL,
[BatchID]             INT          NULL,
[CardID]              INT          NULL,
[UserID]              INT          NULL,
[TransactionDateTime] DATETIME     NOT NULL,
[TransactionStatus]   VARCHAR (11) NOT NULL,
CONSTRAINT [PK_Transactions] PRIMARY KEY CLUSTERED ([TTransactionID] ASC),
CONSTRAINT [FK_Transactions_Cards] FOREIGN KEY ([CardID]) REFERENCES [dbo].[Cards] ([CardID]),
CONSTRAINT [FK_Transactions_Users] FOREIGN KEY ([UserID]) REFERENCES [dbo].[Users] ([UserID]) NOT FOR REPLICATION

这是我的第五次尝试的代码

        var TransactionList = db.Transactions
                                .GroupBy(x => new { x.BatchID })
                                .Select(x => x.ToList()).ToList();

        int index = 0;
        foreach (var item in TransactionList)
        {
            Response.Write( string.Format("[{0}] - {1}", index, item) );
            index++;
        }

当我运行上面的代码时。我在浏览器上收到以下消息。我被困在这里,在此之后该怎么做我不知道。

System.Collections.Generic.List`1[SECardDistribution.Models.Transaction]

请指教 感谢

4 个答案:

答案 0 :(得分:4)

您可以实际使用SelectMany

var transactionList = db.Transactions.GroupBy(x => new { x.BatchID }).SelectMany(x => x).ToList();

答案 1 :(得分:1)

因为您要在LINQ查询中对结果进行分组,所以需要运行两个循环 - 一个用于组,第二个用于组中的数据。

var TransactionList = db.Transactions
                                .GroupBy(x => new { x.BatchID })
                                .Select(x => x.ToList()).ToList();

foreach (var group in TransactionList)
{
   foreach(var item in group) 
   {
       // DO YOUR THINGS HERE...
   }
}

答案 2 :(得分:1)

// 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 :(得分:1)

请注意,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
                }
            }