C#从IList中选择元素

时间:2010-04-11 11:51:40

标签: c# linq

我有一个对象列表IList<O>O有几个属性,但只有两个属性相关:DateDuration

我希望将列表“拆分”为多个列表,这些列表仅包含具有匹配日期和持续时间属性的对象。

示例:

0- Date==1, Duration==7  
1- Date==1, Duration==7  
2- Date==2, Duration==7  
3- Date==2, Duration==7  
4- Date==2, Duration==14  
5- Date==2, Duration==14  

期望的结果(IList&lt; IList&lt; O&gt;&gt;):

0-  
 0- Date==1, Duration==7  
 1- Date==1, Duration==7  
1-  
 0- Date==2, Duration==7  
 1- Date==2, Duration==7  
2-  
 0- Date==2, Duration==14  
 1- Date==2, Duration==14  

我知道这可以通过一些LINQ选择来完成,但我不确定如何。

4 个答案:

答案 0 :(得分:4)

您可以使用以下内容:

var query = 
    from item in list
    group item by new { item.Date, item.Duration } into g
    select g.toList();
var result = query.toList();

这将创建一个具有您要分组的属性的匿名类。然后,它会将IGrouping扩展为原始类型的List。然后运行查询以生成外部List

答案 1 :(得分:2)

有关嵌套的LINQ结果,请参阅grouping operator

以下是我链接到的网站的示例:

public void Linq40()
{
    int[] numbers = { 5, 4, 1, 3, 9, 8, 6, 7, 2, 0 };

    var numberGroups =
        from n in numbers
        group n by n % 5 into g
        select new { Remainder = g.Key, Numbers = g };

    foreach (var g in numberGroups)
    {
        Console.WriteLine("Numbers with a remainder of {0} when divided by 5:", g.Remainder);
        foreach (var n in g.Numbers)
        {
            Console.WriteLine(n);
        }
    }
}

答案 2 :(得分:2)

我正在学习Linq,所以我试图找到解决方案:

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

namespace so_listobj
{
    class Program
    {
        static void Main(string[] args)
        {
            List<O> anOList = new List<O>();
            List<List<O>> aGroupOList = new List<List<O>>();

            anOList.Add(new O(1, 7));
            anOList.Add(new O(1, 7));
            anOList.Add(new O(2, 2));
            anOList.Add(new O(2, 2));
            anOList.Add(new O(2, 14));
            anOList.Add(new O(2, 14));

            Console.Out.WriteLine("Initial state");

            foreach (O o in anOList)
            {
                Console.Out.WriteLine(o);
            }

            var grp =
                from o in anOList
                group o by o.Date into a
                select new {Date = a.Key, aGroupOList = a  };

            Console.Out.WriteLine("after grouping");

            foreach (var _ob in grp)
            {
                foreach (var _anotherOList in _ob.aGroupOList)
                {
                    Console.Out.WriteLine("{0} {1}", _ob.Date, _anotherOList.ToString());
                }
            }
        }
    }


    class O
    {
        private int  _Odate;
        private int _Oduration;

        public O(int date, int duration)
        {
            _Odate = date;
            _Oduration = duration;
        }

        public int Date
        {
            get { return _Odate; }
            set { _Odate = value; }
        }
        public int Duration
        {
            get { return _Oduration; }
            set { _Oduration = value; }
        }


        public override String ToString()
        {
            return String.Format("- Date :{0}\t Duration:{1}", _Odate, _Oduration);
        }
    }
}

答案 3 :(得分:1)

这会给你一个IEnumerable<IGrouping<Date, O>>,它接近你想要的。

list.GroupBy(i => new { Date = i.Date, Duration = i.Duration });

要获得实际的IList<IList<O>>,您需要通过IEnumerable<O>中的IGrouping进一步优化:

list.GroupBy(i => new { Date = i.Date, Duration = i.Duration })
    .Select(g => g.ToList()).ToList();