Click here to Skip to main content
15,885,985 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
We have arraylist like below:

ArrayList list = new ArrayList();
list[0]=""mdmf4": {"min_price": 39.72,"trans": "Manual"}";
list[1]=""mdmf4": {"min_price": 40.72,"trans": "Manual"}";
list[2]=""mdmf5": {"min_price": 39.56,"trans": "Manual"}";
list[3]=""mdmf5": {"min_price": 22.44,"trans": "Manual"}";
list[4]=""maaf4": {"min_price": 71.82,"trans": "Manual"}";
list[5]=""maaf4": {"min_price": 39.00,"trans": "Manual"}";



I want to filter aaraylist with minimum price and result looks like :

list[0]=""mdmf4": {"min_price": 39.72,"trans": "Manual"}";
list[1]=""mdmf5": {"min_price": 22.44,"trans": "Manual"}";
list[2]=""maaf4": {"min_price": 39.00,"trans": "Manual"}";


How to do without using Loop? Is it possible with LINQ?
Posted
Updated 23-Aug-15 21:16pm
v2
Comments
Maciej Los 24-Aug-15 3:16am    
What have you tried? Where are you stuck?
Maciej Los 24-Aug-15 3:20am    
At the first look, aAbove code won't compile...
DamithSL 24-Aug-15 3:46am    
what is your filter condition?
F-ES Sitecore 24-Aug-15 4:19am    
If you want to filter using Linq then don't use an ArrayList, use something that is strongly typed. Also as others have said, your code doesn't look valid, it seems like you're trying to store a list of anonymous types, but as I hinted at above, you should store concrete types in a List<T> or similar.

1 solution

First: I cannot make much sense of your list. Is that supposed to be a JSON style object? if so then it is badly formed.
I will assume a POCO with id and value for this example.

You need a groupby followed by a min:

C#
var result = list
            .GroupBy(l=>l.id)
            .Select(g=>new {id=g.key,value = g.min(l=>l.value);


this will return an anon type with id and value. You can select a new defined POCO type if that's what you need.
 
Share this answer
 
Comments
Maciej Los 24-Aug-15 4:27am    
Good point, a 5!

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900