简体   繁体   中英

using LINQ to select items from a list in groups

I have a model that has the following structure;

{
   Name: // .... //,
   Id: // ... //,
   Tab: "tabName",
   // ... other properties ... //
}

Is it possible to take an IList<T> of this type, and extract out the items into groups by the Tab property? So that I could end up with a shape like this using only a LINQ query?

{
 "tabName1": [ item1, item4, item8 ],
 "tabName2": [ item2, item5, item9 ],
 "tabName3": [ item3, item6, item10]
}

I assume the data structure is something like this:

var obj = new[] { 
 new { Name = "ss", Id = 1, Tab = "tabName1" },
 new { Name = "ss", Id = 1, Tab = "tabName1" },
 new { Name = "ss", Id = 1, Tab = "tabName1" },
 new { Name = "ss", Id = 1, Tab = "tabName2" },
 new { Name = "ss", Id = 1, Tab = "tabName1" }
};

You can use group by this way:

var query = from s in obj
            group s by s.Tab into newGroup
            select newGroup;

And using method syntax:

var query = obj.GroupBy(p => p.Tab).Select(p => new { items = p });

You can do this by the GroupBy method. Below I've set up a demo for you. The code will result in a list. Every object in that list will have a key and an items property. The key will be the tabname and the itemsproperty will contain a list with items.

Class

class TestObj
{
    public int Id { get; set; }
    public string Name { get; set; }
    public string Tab { get; set; }
}

Example

List<TestObj> obj = new List<TestObj>()
{
     new TestObj() { Id = 1, Tab = "tab1", Name = "name1" },
     new TestObj() { Id = 2, Tab = "tab1", Name = "name2" },
     new TestObj() { Id = 3, Tab = "tab1", Name = "name3" },
     new TestObj() { Id = 4, Tab = "tab2", Name = "name4" },
     new TestObj() { Id = 5, Tab = "tab2", Name = "name5" },
     new TestObj() { Id = 6, Tab = "tab4", Name = "name6" },
     new TestObj() { Id = 7, Tab = "tab3", Name = "name7" },
     new TestObj() { Id = 8, Tab = "tab3", Name = "name8" },
     new TestObj() { Id = 9, Tab = "tab3", Name = "name9" },
 };

 var list = obj.GroupBy(x => x.Tab).Select(x => new { key = x.Key, items = x }).ToList();

Try something like this

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace ConsoleApplication1
{
    class Program
    {
        static void Main(string[] args)
        {
            List<Something> somethings = new List<Something>() {
                new Something() { tab = 1},
                new Something() { tab = 2},
                new Something() { tab = 3},
                new Something() { tab = 4},
                new Something() { tab = 5},
                new Something() { tab = 6},
                new Something() { tab = 7},
                new Something() { tab = 8},
                new Something() { tab = 9}
            };


            var result = somethings.Take(3).Select((x, i) => somethings.Where((y, ii) => (ii % 3) == i).Select(z => z.tab).ToArray()).ToArray();


        }
    }
    public class Something
    {
        public int tab { get; set; }
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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