How do I LINQify this?
Resharper says I can but I don't see how. There are several examples of the form:
foreach (ItemType Item in ListOfItems)
if (ConditionalInv开发者_如何学JAVAolvingItem)
Total += ItemProperty;
Of course I could make a sublist on the conditional and then sum the items in the sublist but this would be no clearer and would run slower.
ReSharper is a handy tool, but keep in mind its suggestions aren't always necessarily more performant or self-explanatory. Such is the case here. It's really a toss-up. I'm sure you already have a LINQ statement in mind, but I'd imagine this example would look as such:
var Total = (from Item in ListOfItems
where (ConditionalInvolvingItem)
select ItemProperty).Sum();
var total = listOfItems
.Where(item => ConditionalInvolvingItem(item))
.Sum(item => item.Property);
精彩评论