-1

The problem is: I have a list of objects, with some containing the same PlanId property value. I want to only grab the first occurrence of those and ignore the next object with that PlanId. The root problem is a View in the database, but it's tied in everywhere and I don't know if changing it will break a ton of stuff nearing a deadline, so I'm tossing in a hack for now.

So, if I have a list of PlanObjects like such.

  • Plan1.PlanId = 1
  • Plan2.PlanId = 1
  • Plan3.PlanId = 2
  • Plan4.PlanId = 3
  • Plan5.PlanId = 4
  • Plan6.PlanId = 4

I want to take a sub-list from that with LINQ (italics mean an item is not included)

  • Plan1.PlanId = 1
  • Plan2.PlanId = 1
  • Plan3.PlanId = 2
  • Plan4.PlanId = 3
  • Plan5.PlanId = 4
  • Plan6.PlanId = 4

For my needs, it doesn't matter which one is taken first. The Id is used to update a datbase record.

If I didn't explain that well enough, let me know and I'll edit the question. I think it makes sense though.

Yatrix
  • 12,038
  • 13
  • 39
  • 73
  • Thx for down-voting without explanation. If the idea is to make questions as helpful as possible, suggestions on how to make them better would be a good idea, if you're going to downvote. – Yatrix May 14 '12 at 15:14

2 Answers2

1
PlanObjects.GroupBy(p => p.PlanId).Select(r => r.First());
Didier A.
  • 3,650
  • 2
  • 35
  • 38
0

The other answer (and its comments) supplies the fluent interface solution. Here's the query syntax:

From p In PlanObjects Group By p.PlanId Into First Select First
Mark Hurd
  • 10,175
  • 10
  • 62
  • 96