Linq Query Group and First Element Selection

I have an array of strings like this:

// icon, category, tool String[,] subButtonData = new String[,] { {"graphics/gui/brushsizeplus_icon", "Draw", "DrawBrushPlus"}, {"graphics/gui/brushsizeminus_icon", "Draw", "DrawBrushMinus"}, {"graphics/gui/freedraw_icon", "Draw", "DrawFree"}, {"graphics/gui/linedraw_icon", "Draw", "DrawLine"}, {"graphics/gui/rectangledraw_icon", "Draw", "DrawRectangle"}, {"graphics/gui/ellipsedraw_icon", "Draw", "DrawEllipse"}, {"graphics/gui/brushsizeplus_icon", "Brusher", "BrusherBrushPlus"}, {"graphics/gui/brushsizeminus_icon", "Brusher", "BrusherBrushMinus"}, {"graphics/gui/brushsizeplus_icon", "Text", "TextBrushPlus"}, {"graphics/gui/brushsizeminus_icon", "Text", "TextBrushMinus"}, }; 

Then I populate the List<Button> with my Button type named mainButtons

This is how I request to group for Category :

 var categories = from b in mainButtons group b by b.category into g select new { Category = g.Key, Buttons = g }; 

How can I select the first element of each group in my main list? (without repeating each and adding to another list?)

+42
c # linq
Aug 05 2018-11-11T00:
source share
4 answers

See LINQ: How to Get Last / Last Record Using Group by Clause

 var firstItemsInGroup = from b in mainButtons group b by b.category into g select g.First(); 

I assume that mainButtons are already sorted correctly.

If you need to specify a custom sort order, use OrderBy override with Comparer.

 var firstsByCompareInGroups = from p in rows group p by p.ID into grp select grp.OrderBy(a => a, new CompareRows()).First(); 

See an example in my post Select First Row In Group with Custom Comparer "

+53
Sep 17 '11 at 16:21
source share
 var result = list.GroupBy(x => x.Category).Select(x => x.First()) 
+45
Apr 09 '13 at 16:32
source share

First of all, I would not use a multidimensional array. Only ever seen bad things from it.

Set your variable as follows:

 IEnumerable<IEnumerable<string>> data = new[] { new[]{"...", "...", "..."}, ... etc ... }; 

Then you just go:

 var firsts = data.Select(x => x.FirstOrDefault()).Where(x => x != null); 

In case you have an empty list as an element inside, make sure that it trims any values.

Alternatively, you can implement it as:

 string[][] = new[] { new[]{"...","...","..."}, new[]{"...","...","..."}, ... etc ... }; 

This can be used similarly to the [x,y] array, but it is used as follows: [x][y]

+5
Aug 05 2018-11-11T00:
source share
 var results = list.GroupBy(x => x.Category) .Select(g => g.OrderBy(x => x.SortByProp).FirstOrDefault()); 

For those who are wondering how to do this for groups that are not necessarily sorted correctly, here is an extension of this answer that uses the syntax of the method to adjust the sort order of each and, therefore, get the desired entry from each.

Note. If you use LINQ-to-Entities, you will get a runtime exception if you use First () instead of FirstOrDefault () here, since the first can only be used as the final query operation.

+1
Jul 27 '17 at 15:08
source share



All Articles