亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

如何遍歷System.Collections.Generic.List`1

如何遍歷System.Collections.Generic.List`1

C#
鴻蒙傳說 2021-05-19 17:34:07
我無法從Google搜索中獲得明確的指導。我有一個名為的表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]請告知謝謝
查看完整描述

3 回答

?
一只名叫tom的貓

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

                }

            }


查看完整回答
反對 回復 2021-05-23
?
海綿寶寶撒

TA貢獻1809條經驗 獲得超8個贊

你可以SelectMany實際使用

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



查看完整回答
反對 回復 2021-05-23
?
慕俠2389804

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");

    }

}


查看完整回答
反對 回復 2021-05-23
  • 3 回答
  • 0 關注
  • 870 瀏覽

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號