c# - Converting foreach into single pass Linq Group By -
i have collection of items may have 1 or more properties.
i want use linq statement (query syntax) filter , group list in single pass such checks mandatory property first, , looks optional property.
i can't figure out how without first filtering list , going on again optional property.
here's how in foreach statement. (not efficient, illustrating need.)
var usefulboxes = new dictionary<box, int>; foreach (box in cart) { bool boxneeded = false; int somecounter = 0; foreach (prop in box.properties) { if (prop == neededprop) boxneeded = true; else if (boxneeded && prop == optionalprop) somecounter += 1; } if (boxneeded) usefulboxes.add(box, somecounter) }
var usefulboxes= box.where(b=>b.boxproperties.prop==neededprop).tolist();
Comments
Post a Comment