Reputation: 2910
I have a list with the following values:
<event>
<value1>2013-05-04 10:00:00.123Z</value1>
<value2>1111</value2>
<value3>303533F1700DEE80001E8482</value3>
<value4>IN</value4>
</event>
<event>
<value1>2013-05-04 10:00:00.123Z</value1>
<value2>1111</value2>
<value3>303533F1700DEE80001E8482</value3>
<value4>OUT</value4>
</event>
I need to group the value3 values and get the Distincts value4.
The difficulty is that its can come mixed, with the the value4 such: "OUT, OUT, IN, OUT, IN, IN".
Is this possible to do in Linq?!
I got this so far:
var s = (from p in trackingList
group p by new {p.value3}
into grp
select new TrackingSteps
{
DateIn = **grp.Key.value1**,
DateOut = 'the **value1** of **value4**'
}).ToList();
Upvotes: 3
Views: 998
Reputation: 12811
I'm going to attempt an answer, but it's not entirely clear what you're going for. If you are attempting to take a set of objects and produce a single object for each value3, where each of the produced objects has properties DateIn = (MIN value1 where value4 = "IN") and DateOut = (MAX value1 where value4 = "OUT"), then it might look something like this:
from p in trackingList
group p by p.Value3 into g
select new {
Value3 = g.Key,
DateIn = g.Where(o => o.Value4 == "IN")
.Select(o => o.Value1)
.DefaultIfEmpty(DateTime.MinValue)
.Min(),
DateOut = g.Where(o => o.Value4 == "OUT")
.Select(o => o.Value1)
.DefaultIfEmpty(DateTime.MaxValue)
.Max()
}
Upvotes: 2
Reputation: 1848
This LINQ query groups by Value3 and select distinct Value4 for each group
var result = trackingList.Descendants("event")
.Select(E => new { Value1 = E.Element("value1").Value,
Value3 = E.Element("value3").Value,
Value4 = E.Element("value4").Value })
.GroupBy(g => g.Value3)
.Select(g => new {
Value3 = g.Key,
//DistintValue4 = g.Select(x => x.Value4).Distinct(),
DateIn = g.Where(x => x.Value4 == "IN").Select(x => x.Value1).FirstOrDefault(),
DateOut = g.Where(x => x.Value4 == "OUT").Select(x => x.Value1).FirstOrDefault()
});
Upvotes: 1