簡體   English   中英

如何對ConcurrentBag進行排序 <Tuple<string,int,decimal> &gt;()由C#中的Item3提供?

[英]How to sort ConcurrentBag<Tuple<string,int,decimal>>() by Item3 in c#?

我有一個ConcurrentBag<Tuple<string,int,decimal,string>> pricelist並想按pricelist.Item3對其進行排序。

我以前只有一個List<Tuple<string,int,decimal,sting>> priceList並使用以下方式對其進行排序:

priceList.Sort((x, y) => x.Item3.CompareTo(y.Item3));

由於多線程處理,我用ConcurrentBag交換了它,但我不知道如何對並發包進行排序。

因此,請幫助我如何按Item3priceList進行排序

這是我當前的偽代碼:

    ConcurrentBag<(string intemID, int amount, decimal price, string seller)> temporaryPriceList = new ConcurrentBag<(string, int, decimal, string)>();
    Parallel.ForEach(cardProductIDs, (productID) =>
    {
        int indexCondition = GlobalVar.cardConditions.IndexOf(card.condition);
        foreach (var price in task.GetProductSellers(int.Parse(productID), minUserScore, idLanguage, indexCondition))
        {
            temporaryPriceList.Add(price);
        }
    });
    temporaryPriceList.OrderBy(element.price);

這是我以前的工作代碼:

    List<Tuple<string, int, decimal, string>> temporaryPriceList = new List<Tuple<string, int, decimal, string>>();
    // add sellers for productcategory
    foreach (string productID in cardProductIDs)
    {
        int indexCondition = GlobalVar.cardConditions.IndexOf(card.Item3);
        temporaryPriceList.AddRange(GetProductSellers(int.Parse(productID), minUserScore, idLanguage, indexCondition));
    }
    // sort
    temporaryPriceList.Sort((x, y) => x.Item3.CompareTo(y.Item3));
    // Process with the cheapest sellers

您可以通過以下方式對數據進行排序:

temporaryPriceList.ToArray()
.OrderBy(v=>v.Item3)
.ToArray()

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM