0

I have a list of arrays with 2 strings in each array. I need to check if the first string in each array matches a given number. I am assuming that linq is the best way to do this. I found a helpful SO answer here: Find an item in List by LINQ? It states to find an item in a list do this:

string result = myList.Single(s => s == search);

From the comments, I think I want to use SingleOrDefault. But how do I make it search the first item of each array in the list? Here is my list of arrays:

List<string[]> shipsLater = new List<string[]> {};    
string[] itemArr = { item.pid, future };
shipsLater.Add(itemArr);
7
  • what do you want as result? Mar 4, 2015 at 18:13
  • I want to search all of the item.pid for a given string, such as "9022".
    – dmikester1
    Mar 4, 2015 at 18:13
  • Simplest would be shipsLater[0][0] == shipsLater[1][0] ;) Mar 4, 2015 at 18:15
  • you still didnt answer my question, what should be the result? list of arrays? a single array? a string? Mar 4, 2015 at 18:16
  • oh, basically just true or false, did i find it or not
    – dmikester1
    Mar 4, 2015 at 18:24

3 Answers 3

3

So you have a List of arrays like:

List<string[]> list = new List<string[]>();

Now each array consist of two elements, and you want to compare if first element is equal to your search parameter. You can do:

var query = list.Where(arr=> arr.First() == search);

This will give you all those element in the list which matches your condition.

From your comment:

basically just true or false, did i find it or not

If you are only looking to get back a boolean result indicating whether the condition has met or not use Enumerable.Any like:

bool result = list.Any(arr=> arr.First() == search);

if your parameter is of type int then call ToString like:

bool result = list.Any(arr=> arr.First() == search.ToString());
2
  • There can only be one or none. Can I use SingleOrDefault instead of Where?
    – dmikester1
    Mar 4, 2015 at 18:17
  • @dmikester1, just to be clear, you mean there will be one item in the list which would match the condition ? if that is the case sure use SingleOrDefault, but it will return a string[] where first item would match your search criteria.
    – Habib
    Mar 4, 2015 at 18:17
1

You can use Dictionary() for best performance result; if you want to use string[] use this:

string result = myList.Single(s => s[0] == search);
0

Your initial array structure is problematic. Careful conversion to a dictionary suddenly makes the implementation simple.

List<string[]> shipsLater = new List<string[]>
{
  new []{ "pid1", "abc" },
  new []{ "pid1", "xyz" },
  new []{ "pid2", "123" }
};

Dictionary<string, IEnumerable<string>> lookupByPid = shipsLater
  .GroupBy(g => g[0])
  .ToDictionary(k => k.Key, v => v.Select(i => i[1]));

// now your lookups are as simple as...
IEnumerable<string> matches = lookupByPid["pid1"];

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.