I have a list that looks something like this:
catId itemId value
1 5 "some text"
1 19 "some text"
1 21 "some text"
2 6 "some text"
2 8 "some text"
3 9 "some text"
3 82 "some text"
Notice the catId column. How would I take 2 rows(or remove extra rows) for each unique value in the first colum. Only 2 rows for catId=1 2 rows for catId=2 and so on?
My list is called distinctXSelling and I’m using it like this currently:
foreach (var item in distinctXSelling)
{
xSelling.InnerHtml += item.value;
}
You can group items by their category-id and then pick the first two items from each group, before flattening the sequence of groups into a sequence of source-items. This would look like:
Note that the
Takemethod takes upto a certain number of items, so this won’t throw if a category has only a single item.As BrokenGlass points out, you might need a
.OrderBy(and possibly a.ThenBy) if you want the filtered items out in a specific order.You might also want to consider replacing the loop with: