简体   繁体   中英

How to get distinct values from List<Dictionay<string, string>> in c#

Due to limitations on the system I am getting data from, I end up with duplicate Dictionaries in a orders list. The system looks at the amount of items and gives me one order for each item inside the order. Basically I end up with this:

            List<Dictionary<string, string>> myOrders = new List<Dictionary<string, string>>();

            Dictionary<string, string> dictionay = new Dictionary<string,string>();
            dictionay.Add("OrderId", "00000082");
            dictionay.Add("Order Status", "In Process");
            myOrders.Add(dictionay);
            Dictionary<string, string> dictionay1 = new Dictionary<string, string>();
            dictionay1.Add("OrderId", "00000082");
            dictionay1.Add("Order Status", "In Process");
            myOrders.Add(dictionay1);
            Dictionary<string, string> dictionay2 = new Dictionary<string, string>();
            dictionay2.Add("OrderId", "00000083");
            dictionay2.Add("Order Status", "In Process");
            myOrders.Add(dictionay2);
            Dictionary<string, string> dictionay3 = new Dictionary<string, string>();
            dictionay3.Add("OrderId", "00000084");
            dictionay3.Add("Order Status", "In Process");
            myOrders.Add(dictionay3);

I am sure there is a simple way to do this but I am having a hard time to loop through the myOrders and compare the OrderId value in order to end up with distinct dictionaries within the list. Can anyone help?

First off - If each list item is a single entry, you might want to consider switching to List<Tuple<string,string>> or List<KeyValuePair<string,string>> .

That being said, you could get a unique list of OrderId values via something like:

var uniqueIds = new HashSet<string>(
                     myOrders
                     .SelectMany(o => o) // Extract all KeyValuePairs
                     .Where(kvp => kvp.Key == "OrderId")
                     .Select(kvp => kvp.Value));

I'd suggest first selecting out the distinct IDs:

IEnumerable<string> ids = myOrders.Select(order => order["OrderId"]).Distinct();

Then selecting the first dictionary corresponding to each id:

List<Dictionary<string, string>> distinctOrders = 
    ids.Select(id => 
        myOrders.First(order => order["OrderId"] == id)).ToList();

I'd also suggest you shouldn't use Dictionaries for this purpose and should instead create an Order class with OrderId and OrderStatus as properties on that class.

Alternatively, if you don't mind adding a third-party dependency, you might consider morelinq , which has a DistinctBy method suitable for this. If you use that, you'd simply do:

List<Dictionary<string, string>> distinctOrders = 
    myOrders.DistinctBy(order => order["OrderId"]).ToList();

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM