Click here to Skip to main content
15,910,234 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I have following item objects in a List<Items>.

obj1 - ItemCode=001;
Name="aa";
Price=100;
PoNo=1;

obj2 - ItemCode=002;
Name="bb";
Price=200;
PoNo=2;

obj3 - ItemCode=001;
Name="aa";
Price=100;
PoNo=3;

obj4 - ItemCode=001;
Name="aa";
Price=250;
PoNo=4;

Now I want to take unique ItemCode and Price in PoNo. If same ItemCode and Price contains in different PoNo, Max PoNo should take.

Finaly output list should contains.
obj2,obj3,obj4
Posted
Comments
Sergey Alexandrovich Kryukov 15-Nov-11 0:52am    
Where is you LINQ here?
--SA

You are doing it wrong. As I can see, you need fast search by ItemCode. If so, don't use List.

Assuming the declaration is something like
C#
class Item {
   internal int ItemCode { get; set; }
   internal string Name { get; set; }
   //...
}


store your items in System.Collections.Generic.Dictionary<ItemCode, Item>, that is, the dictionary of Items indexed by ItemCode, see http://msdn.microsoft.com/en-us/library/xfhwa508.aspx[^].

Alternatively, use generic SortedDictionary or SortedList from the same name space. You want get any functional difference, only different trade-off between memory overhead and speed, pretty good in all cases, with complexity of O(1) (for understanding "Big O Notation", see http://en.wikipedia.org/wiki/Big_O_notation[^]).

—SA
 
Share this answer
 
Comments
BillWoodruff 15-Nov-11 8:57am    
In this case there are multiple items ... three of them ... with identical ItemCode values: so that excludes using those as Keys in a Dictionary.

But, I share what I believe is your underlying idea that some other form of data structure may be more useful here; imho, to help the OP design/optimize that structure we need to consider frequency of access by type of access.
Sergey Alexandrovich Kryukov 5-Apr-12 2:27am    
Thank you, Bill.
Things are simple: there is a trade-off between redundancy and search speed. You correctly point out that the decision depends on frequency, but it also depends on the volume of data, requirements for speed of calculations and other factors.
--SA
Please describe this in more detail.

It appears to me you want a final result list that includes one of every object which either has a unique ItemCode ... or ... if there are multiple Items with the same ItemCode and which have all internal fields except PoNo match in Value ... then you wish to select one of those multiple items.

And, when two (or more) Items match in every way but the PoNo: you need make explicit ... if you are only choosing one of them ... what the basis for that choice is: first ? last ?

Based on the observation that Item1 and Item3 share all values except PoNo, and Item1's not in your final list result, I assume you want to take only the last unique instance with Itemcode 001 ... is that correct ?

Some other form of data structure than generic List may be a good fit here, but in order to discuss that, I think we need to know something about how the objects are accessed and used: what's the most frequent type of use here ?

If, as shown here, PoNo is a unique identifier for each object, that's a start.
 
Share this answer
 
C#
var result = (
            from f in foos
            from s in f.MyStrings
            select s).Distinct();

        // Which is absoulutely equivalent to:

        var theSameThing = foos.SelectMany(i => i.MyStrings).Distinct();

        // pick the one you think is more readable.
 
Share this answer
 
 
Share this answer
 

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