Using LINQ to GroupBy various properties and Sum length

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report
Hello,
I am trying to use LINQ to organize all the Pipes in the model by System, Segment, then by Size and then sum the pipe length. I grab all the pipes in the model and get the System, Segment, Size and Length and put all that in to an IEnumerable. I then pass the IEnumerable through a LINQ operation to do GroupBy and Sum the Lengths of pipe for similar sized pipes with the same segment on the same system. Here is my code for the function that tries to do the GroupBy and Sum. I am not sure why I am getting an error that says "Cannot implicitly convert type 'double' to 'bool'" for the count operation so I have it commented out:
// Sum the length of pipe by system, segment, then size. Count pipes for each system/segment/size for checking private IEnumerable<classPipesBySystem> sumPipeLength(List<pipesBySystem> theList) { var sumPipeLengthList = theList.GroupBy(s => new { s.pipeSystem.Name, s.pipeSegment, s.pipeSize }) .Select(g => new classPipesBySystem { pipeSystemName = g.Key.Name, pipeSegment = g.Key.pipeSegment, pipeSize = g.Key.pipeSize, totalPipeLength = g.Sum(x => x.pipeLength), //pipeCount = g.Count(x => x.pipeLength) }); return sumPipeLengthList; }
Here is the call to the above function that passes in the IEnumerable. I was trying to iterate through the GroupBy Lists since it turns in to a Nested List but I get an error on the second foreach statement saying my classPipesBySystem class does not contain a public definition of GetEnumerator.
// Send the pipeSysInfo list to sumPipeLength to total the pipe length IEnumerable<classPipesBySystem> sumPipeLengthInfo = sumPipeLength(pipeSysInfo); foreach (var systemGroup in sumPipeLengthInfo) { foreach (var segmentGroup in systemGroup) // Error about missing GetEnumerator definition in classPipesBySystem appears on this foreach { foreach (var sizeGroup in segmentGroup) { // Build a string listing each unique pipe size, segment, and system with total length } } }
I'm not sure why it errors only on the second foreach and how I go about adding the GetEnumerator definition. Is the IEnumerable data type the correct type to use for the Nested List variable? Is there a better way to group and sum the pipe lengths for each size by segment and system other than using LINQ GroupBy?
Thanks,
Joe