How can I optimize this LINQ statement for speed?
The following LINQ statement finds items with duplicate values for a particular property, groups them by count then does another grouping to get a boolean back.
I'm just curious how this can be improved, it seems a bit wasteful and as this is part of a object model validation library I'd quite like to get it as speedy as I can.
Speed of execution is a priority, but any other suggestions are welcome.
var grouped = from g2 in
(from i in item.ParentList
开发者_JS百科 where _filter(i)
group i by propGetter(i) into g
select new { Count = g.Count(), Items = g })
group g2 by g2.Count == 1 into g3
select new { IsUnique = g3.Key, Items = g3 };
foreach (var g in grouped)
{
foreach (var grp in g.Items)
{
foreach (var itm in grp.Items)
{
if (g.IsUnique == false)
itm.AddPropertyError(_propertyName, (int)Validations.Unique, _message);
else
itm.RemovePropertyError(_propertyName, (int)Validations.Unique);
}
}
}
As Oskar said, you probably don't want to optimize the query for speed unless profiling shows that it's causing a problem. Premature optimization is the root of all evil. If you want to optimize the query for readability, here is one way to simplify the second part:
var items = grouped
.SelectMany(group => group.Items)
.SelectMany(group => group.Items)
foreach (var item in items)
{
...
}
Edit regarding your comment: Ah, I hadn't noticed that you were referencing g.IsUnique
inside the innermost loop. Here is one way to resolve that problem without 3 levels of indentation, but it may not be the best way:
var uniqueItems = grouped
.Where(group => group.IsUnique)
.SelectMany(group => group.Items)
.SelectMany(group => group.Items)
var nonUniqueItems = grouped
.Where(group => !group.IsUnique)
.SelectMany(group => group.Items)
.SelectMany(group => group.Items)
foreach (var item in uniqueItems)
{
...
}
foreach (var item in nonUniqueItems)
{
...
}
精彩评论