0

I am having the hardest time coming up with a methodology to take a list of non-unique Selectlistitems and come out with a list of unique SelectListItems. I am attempting to use linq to do this... The code for what I tried was this

    var queryResults =
    from p in PatientList
    group p by p.Value into g
    select new SelectListItem { Text= g.Key, Value= g.Max(p => p.Value.Split('|')[1]) };

With this methodology I get the exact same list that I went in with. Now I am a complete novice when it comes to linq. I have done some linq to XML and linq to SQL, but Linq to Collections seems to be alluding...

Any Help possible?

SoftwareSavant
  • 9,467
  • 27
  • 121
  • 195

1 Answers1

2

Perhaps you could use Distinct()? It returns unique values from a sequence, optionally using an IEqualityComparer<T> for the uniqueness test.

stuartd
  • 70,509
  • 14
  • 132
  • 163
  • I didn't think of that. Let me give that a try. – SoftwareSavant Sep 08 '11 at 13:46
  • As far as I know, Distinct() will check the hash code on the objects in the collection and filter based on that. If the duplicate SelectListItems in the collection are not actually the same objects but other objects that just look the same you'll find you end up with the same list. – Jamie Dixon Sep 08 '11 at 16:39
  • MoreLinq (http://code.google.com/p/morelinq/) provides a DistinctBy method in which you can pass a comparison lambda: items.DistinctBy(c => c.Value) – Jamie Dixon Sep 08 '11 at 16:41
  • I am not to sure how that would help me. This appears to be an intractable problem. – SoftwareSavant Sep 09 '11 at 12:06