2

Let's say I have the following data,

data: {
    variations: [{
        steps: [
            { Name: "Crawl", Status: "Complete" },
            { Name: "Walk", Status: "InProgress" }
        ]
    },{
        steps: [
            { Name: "Crawl", Status: "Complete" },
            { Name: "Walk", Status: "Complete" },
            { Name: "Run", Status: "NotStarted" }
        ]
    }]
}

How would I arrive at this set of data using linq.js? The resulting set of data is the unique steps across all variations. Notice, the duplicate Crawl is not in the result.

[
    { Name: "Crawl", Status: "Complete" },
    { Name: "Walk", Status: "InProgress" },
    { Name: "Walk", Status: "Complete" },
    { Name: "Run", Status: "NotStarted" }
]

I have tried many combinations of Select and SelectMany, but I'm having no luck.

Scott Lin
  • 1,532
  • 1
  • 18
  • 28

1 Answers1

4

First you'll need to flatten to an array of steps. Once you have that, you'll have to pick out the distinct copies of the steps. Since you're dealing with objects, you'll need to provide a comparer. I would just combine the properties that make it distinct into a string.

var query = Enumerable.From(result.data.variations)
  .SelectMany("$.steps")
  .Distinct("[$.Name, $.Status].join(',')")
  .ToArray();
Jeff Mercado
  • 129,526
  • 32
  • 251
  • 272