Linq Query Group By and Selecting First Items

后端 未结 4 404
余生分开走
余生分开走 2020-11-29 02:59

I have a String array kinda like this:

// icon, category, tool
String[,] subButtonData = new String[,]
{
    {\"graphics/gui/brushsizeplus_icon\", \"Draw\",          


        
4条回答
  •  佛祖请我去吃肉
    2020-11-29 03:54

    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> 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.

    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]

提交回复
热议问题