I want to take the results of a where clause on a list and then take that result set and create just one new type that has all its fields constructed from aggregates of the original query. So given the basic example below, is there anyway to combine the 2 linq statements into one? If the original where has no rows then it should return null. Thanks!
class Foo
{
public int A { get; set; }
public int B { get; set; }
}
List<Foo> lst = GetFooList();
var q = (from f in lst
where f.A > 3
select f).ToList();
if (q.Count != 0)
{
var qq = new
{
MinA = q.Min(l => l.A),
MaxB = q.Max(h => h.B),
};
// now do something with qq
}
Update:
For my situation, the original set has lots of items but after the where clause the result set is very small. Enumerating over the second set several times should not be a problem. Also I need to use first and last on the set to get a value from those records. The group by answer will work best for me. The aggregate way is very interesting and I think have another use for that.
It's relatively tricky, because by the time you've tested whether or not there is an element, you've consumed that first element. I suspect there's a way round it, but it's likely to be a bit hacky… Will think more.
This solution iterates the list only once with
Aggregate()
, but for empty lists it will return the seed value. By the way, the seed values areint.MaxValue
andint.MinValue
becauseMath.Min(int.MaxValue, C)
will always return C and likewiseMath.Max(int.MinValue, C)
will always return C.