C# select elements from IList
I have a list of objects, IList<O>
.
O
has several properties but only two of them are relevant: Date
and Duration
.
I want to "split" the list into several lists that contain only the objects that have matching Date and Duration Properties.
Example:
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
Desired result (IList<IList<O>>):
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
I know this can be done with some LINQ selects but am not sure how.
You can use the following:
var query =
from item in list
group item by new { item.Date, item.Duration } into g
select g.toList();
var result = query.toList();
This creates an anonymous class with the properties that you want to group by. It then expands the IGrouping
to a List
of the original type. The query is then run to produce the outer List
.
See the grouping operator for nested LINQ results.
Here is an example from the site I linked to:
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);
}
}
}
I am learning Linq, so I tried to find out a solution:
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);
}
}
}
This will give you an IEnumerable<IGrouping<Date, O>>
, which is close to what you want.
list.GroupBy(i => new { Date = i.Date, Duration = i.Duration });
To get an actual IList<IList<O>>
, you'll have to further refine by getting the IEnumerable<O>
out of the IGrouping
:
list.GroupBy(i => new { Date = i.Date, Duration = i.Duration })
.Select(g => g.ToList()).ToList();
精彩评论