I have a list and want to find values in it. Search text to find the values will be in Comma separated string.
Input:
var students = new List<Student>() {
new Student(){ Id = 1, Name="One"},
new Student(){ Id = 2, Name="Two"},
new Student(){ Id = 3, Name="Three"},
new Student(){ Id = 4, Name="Four"},
new Student(){ Id = 5, Name="Five"}
};
Search Text:
var searchString = "Two,Three";
Output:
{Id = 2, Name="Two"},
{Id = 3, Name="Three"}
Thanks in Advance.
2
Answers
First, I’d split the search string and put the values into a HashSet:
Next, check if the elements match, for example using LINQ:
Well, standard loops, raw code, you have this;
Not pretty – but shows looping, and how to pick apart each item.
Next up, a linq expression.
So, I would go with the 2nd.
I "think" there is a relative clean and readable way to pass the search part a "list", and thus even the for/each for the split on search terms could be dropped (would be happy if shown how this is possible).
However, I think the above is readable enough – and easy to adopt.