简体   繁体   English

Linq查询分组和选择第一项

[英]Linq Query Group By and Selecting First Items

I have a String array kinda 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 a List<Button> with my Button Type named mainButtons 然后我使用名为mainButtons Button Type填充List<Button>

This is how I query for grouping for Category : 这是我查询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 item of each group in my main List? 如何在主列表中选择每个组的第一个项目? (without iterating each and adding to another List?) (没有迭代每个并添加到另一个列表?)

var result = list.GroupBy(x => x.Category).Select(x => x.First())

See LINQ: How to get the latest/last record with a group by clause 请参阅LINQ:如何使用group by子句获取最新/最后一条记录

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

I assume that mainButtons are already sorted correctly. 我假设mainButtons已经正确排序。

If you need to specify custom sort order, use OrderBy override with Comparer. 如果需要指定自定义排序顺序,请对Comparer使用OrderBy override。

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 using Custom Comparer " 请参阅我的帖子“使用自定义比较器选择组中的第一行”中的示例

var results = list.GroupBy(x => x.Category)
            .Select(g => g.OrderBy(x => x.SortByProp).FirstOrDefault());

For those wondering how to do this for groups that are not necessarily sorted correctly, here's an expansion of this answer that uses method syntax to customize the sort order of each group and hence get the desired record from each. 对于那些想知道如何为不一定正确排序组做到这一点,这里有一个扩展这个答案 ,使用方法的语法自定义每个组的排序顺序,从而获取每个所需的记录。

Note: If you're using LINQ-to-Entities you will get a runtime exception if you use First() instead of FirstOrDefault() here as the former can only be used as a final query operation. 注意:如果您正在使用LINQ-to-Entities,那么如果您在此处使用First()而不是FirstOrDefault(),则会出现运行时异常,因为前者只能用作最终查询操作。

First of all, I wouldn't use a multi-dimensional array. 首先,我不会使用多维数组。 Only ever seen bad things come of it. 只见过不好的事情。

Set up your variable like this: 像这样设置你的变量:

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

Then you'd simply go: 然后你就去:

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

The Where makes sure it prunes any nulls if you have an empty list as an item inside. 如果你有一个空列表作为一个项目,Where确保它修剪任何空值。

Alternatively you can implement it as: 或者,您可以将其实现为:

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

This could be used similarly to a [x,y] array but it's used like this: [x][y] 这可以类似于[x,y]数组使用,但它的使用方式如下: [x][y]

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM