0

How to search for a specific string in List of string array if I have List like this:

adjustmentJSON.columns = new List<string[]>
{
    new[] {"A|green"}, new[] {"B|green"}, new[] {"A|green"},
    new[] {"DD|green"}, new[] {"|green"}, new[] {"|green"}, new[] {"NN|red"}
};

var skipValue = 0;
var TOTALDYNAMICSECTIONS = 5;

var categorizationByBenefitHeader = adjustmentJSON.columns
    .Skip(skipValue)
    .Take(TOTALDYNAMICSECTIONS)
    .Where(b => adjustmentJSON.columns.Contains(new[] { "green" }))
    .ToList();

I want to get:

["A|green","B|green","A|green","DD|green","|green"]
8
  • What is the type of input data? What does If skipValue = 0,TOTALDYNAMICSECTIONS =4 mean? Commented Jun 9, 2020 at 20:03
  • @PavelAnikhouski I mean I have a list of arrays of strings, so I loop, searching the first 5 items, then the second 5 items and so on because some business logic Commented Jun 9, 2020 at 20:05
  • Do you really want to Skip and Take before you filter the results? Commented Jun 9, 2020 at 20:20
  • @RufusL yeah,I want to do this Commented Jun 9, 2020 at 20:22
  • 1
    Ok, just checking. So the results may be fewer than TOTALDYNAMICSECTIONS Commented Jun 9, 2020 at 20:26

1 Answer 1

3

You can use SelectMany to flatten the nested collections:

var categorizationByBenefitHeader = adjustmentJSON.columns
     .SelectMany(arr => arr)
     .Where(s => s.Contains("green"))
     .Skip(skipValue)
     .Take(TOTALDYNAMICSECTIONS)
     .ToList();
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.