I have the following code:
public class Alert
{
public string Alias { get; set; }
public int ServiceHours { get; set; }
public int TotalHoursDone { get; set; }
public int UserId { get; set; }
public int VehicleId { get; set; }
}
private static readonly List<Alert> AlertsToDo = new List<Alert>();
public void SomeFunction() {
// creates a dictionary of user ids as the key with their alerts
// (sorted by alias) as the value.
// Just one loop needed and no awkward state logic.
var alertsGrouped = AlertsToDo.Select(a => a.UserId)
.Distinct()
.ToDictionary(userId => userId,
userId => AlertsToDo.Where(a => a.UserId == userId)
.OrderBy(a => a.Alias)
.ToList());
}
So, I have a list of Alert objects. My LINQ query outputs a Dictionary the key of which is the UserId and the value of which is a List of Alerts for that UserId sorted by Alias.
I’m happy enough with the ingenuity of this query but am wondering if there’s an easier way to do it? Specifically, I’m having to use a second query for every key to create the value List. Asking purely out of interest as this is fast enough for the job in hand.
There’s a shorter approach that is more readable, using the
Enumerable.GroupBymethod. For a small amount of data you most likely won’t see a difference, whereas a large amount of data would change the performance. Your query first gets the distinct values, then filters items onUserId. The grouping cuts down on those steps upfront. To know for sure you would need to profile.Here’s the query using grouping: