LINQ search though a list of string arrays for a particular string
Solution 1
You can put it all into one query:
someCollection.Where(x => listOfValues.Select(y => y[0]).Contains(x => x.id_num);
But it will iterate over listOfValues
over and over again.
I would rather go with HashSet<string>
to make it faster:
var set = new HashSet<string>(listOfValues.Select(y => y[0]));
someCollection.Where(x => set.Contains(x));
Solution 2
Try the following
var query = someCollection.Where(s => listOfStringArrays.Any(a => a[0] == s));
Solution 3
var firsts = listOfString.Select(x => x[0]);
var query = someCollection.Where(x => firsts.Contains(x));
This will project each array to it's first element, and then match from there
As a one liner:
var query = someCollection.Where(x => listOfString.Select(y => y[0]).Contains(x));
Solution 4
It should be simply:
List<String[]> newListOfStrings = listOfStrings.where(x => x[0].Contains(identifer)).ToList()
The final ToList
is needed in this case because I have not used var
.
dursk
Software developer in NYC, writing lots of Python and Javascript. http://github.com/dursk http://matthewmadurski.com
Updated on December 14, 2020Comments
-
dursk over 3 years
I have a list of string arrays:
List<String[]> listOfStringArrays = something;
I need to select all objects from a collection that have a value which is equal to the string at the 0th index of any string array in the list.
For example, if I just had a simple list of strings, declared as:
List<String> listOfStrings = something;
I would just do:
var query = someCollection.Where(x => listOfStrings.Contains(x.id_num))
But obviously it's not as simple with a list of string arrays.
I know that I can easily just iterate through the list of string arrays and create a simple list of strings with the 0th value, like this:
List<String[]> listOfStringArrays = something; List<String> listOfValues = new List<String>(); foreach (string[] s in listOfStringArrays) listOfValues.Add(s[0]); var query = someCollection.Where(x => listOfValues.Contains(x => x.id_num);
But would really like to avoid this and am trying to write it as a one liner without introducing extra lists and loops.
-
dursk over 10 yearsOk so I left out some details which are getting in the way while using HashSet. The values in the collection I am comparing them to are integers, so I have to do a cast one way or the other.
-
dursk over 10 yearsNevermind, just have to call ToString()...I over analyzed and confused myself because I never used HashSet before.
-
MarcinJuraszek over 10 yearsIf your values are
int
s useHashSet<int>
. -
dursk over 10 yearsWell the values from the collection are int, and the List is of course strings. What I did was: someCollection.Where(x => set.Contains(x.ToString()));
-
MarcinJuraszek over 10 yearsSo the list contains
string
representation ofint
s? That's not good. -
dursk over 10 yearsIt's populated from importing a CSV. It's given to me in this from from another area of the program. After I run this query I never touch it again though, and do all my work on the collection which represents the data correctly, so it's not that bad. I do know what your saying though, but currently don't have the option of going deeper and changing things.