Reputation: 13
What I have is a collection named HourList which is made up of Hour objects. Two of the properties on an Hour are EmployeeId and Hours. Employees punch in and out and an hour record is created. So at the end of a week I am delivered an HourList of mulipple Hour objects for mulitple employees from our DAO. It is requested that a report be made where only employees with total hours above a given threshold are displayed.
Example: A simple and flattened HourList
Id | Hours ---------- 1 | 4.5 2 | 6.0 3 | 9.9 1 | 5.5 2 | 2.5
The threshold is 10. In this case I would only want an Id 1's hour records, since his summed hours exceed 10.
I know I could do this by creating a new DAO method to handle this when I return the initial HourList. I also can do this very inefficiently with a foreach statement. I am trying to get better with Linq though and would like to see what's possible. Thank you for any help in advance.
Upvotes: 1
Views: 192
Reputation: 62057
Here is a quick example that does what you are after:
class Program
{
public class HourEntry
{
public int Id { get; set; }
public int Hours { get; set; }
}
static void Main(string[] args)
{
List<HourEntry> hours = new List<HourEntry>
{
new HourEntry { Id = 1, Hours = 3 },
new HourEntry { Id = 2, Hours = 4 },
new HourEntry { Id = 3, Hours = 3 },
new HourEntry { Id = 1, Hours = 8 },
new HourEntry { Id = 5, Hours = 2 },
new HourEntry { Id = 3, Hours = 2 },
new HourEntry { Id = 3, Hours = 6 },
new HourEntry { Id = 9, Hours = 2 },
new HourEntry { Id = 4, Hours = 2 },
};
var moreThanTen = from h in hours
group h by h.Id into hGroup
where hGroup.Sum(hg => hg.Hours) > 10
select hGroup.Key;
foreach (var m in moreThanTen)
{
Console.WriteLine(m);
}
}
}
Upvotes: 2
Reputation: 16065
var hourList = new List<Hour>()
{
new Hour() { Id = 1, Hours = 4.5M},
new Hour() { Id = 2, Hours = 6M},
new Hour() { Id = 3, Hours = 9.9M},
new Hour() { Id = 1, Hours = 5.5M},
new Hour() { Id = 2, Hours = 2.5M}
};
var over10 = hourList.GroupBy(h => h.Id).Select( h => new { Id = h.Key, Sum = h.Sum( s => s.Hours )}).Where( x => x.Sum >= 10 );
Upvotes: 1
Reputation: 245489
var filtered = HourList.GroupBy(h => h.id)
.Select(g => new { Id = g.Key,
Hours = g.Sum(h => h.Hours)
})
.Where(h => h.Hours >= 10);
Can't test it, but this should get you close.
Upvotes: 4