Question

I have an IGrouping structure

IGrouping<TierRequest,PingtreeNode>

PingtreeNode contains a property of Response which in turn has a property Result.

    public class PingtreeNode
    {
        public ResponseAdapter Response { get; set;}
        // ... more properties
    }

    public class ResponseAdapter
    {
        public int Result { get; set; }
        // ... more properties
    }

What I want to do is check whether PingtreeNode contains any nodes with a Result == 2. I know the answer includes a SelectMany but I'm struggling to get the correct syntax.

Anyone help?

Was it helpful?

Solution

Because you have to check

whether PingtreeNode contains any nodes with a Result == 2

I would use Any method:

IGrouping<TierRequest,PingtreeNode> source;

bool anyResultIs2 = source.SelectMany(x => x)
                          .Any(x => x.Response.Result == 2);

It can also be done without SelectMany:

bool anyResultId2 = source.Any(g => g.Any(x => x.Response.Result == 2));

And because both SelectMany and Any are lazy (return only one element at the time and end execution as soon as result is determined), performance of both approaches should be similar.

OTHER TIPS

Assuming that, as a result, you want a collection of PingTreeNode that satisfies your condition, this should do the trick:

var query = yourstruct.SelectMany(x=>x)
  .Where(x => x.Response.Result == 2);
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top