2

I have an array structured like this:

{"nick", "sender", "message"}

arranged into a List<string[]>.

What I want to do, is to search the list using the 0 index value of the array (ie nick).

I tried using:

list.Find(i => i[0].Equals(nick))

but this doesn't seem to do anything.

How would I do this?

6
  • I have no idea what you mean.. why would you only want to search the first index position? Surely you would just do if(i[0] == nick) for that? Commented Oct 13, 2011 at 9:44
  • so I have a whole slew of values in this list, with lots of different nick values. this is for a memo system of sorts, so I want to retrieve every array from the list with the index 0 value equal to the nick i'm after. so, for example: {"sue", "bob", "dinner at 9"} {"joe", "bob", "park your car"} {"sue", "joe", "bob smells"} and I want the list to return the values which have the value "sue" in index 0 Commented Oct 13, 2011 at 9:45
  • Try this: list.Find(i => i[0] == "nick");. Commented Oct 13, 2011 at 9:48
  • What about using list.Where(i => i[0] == nick) then to get a list of string[] that contain nick at first index? Commented Oct 13, 2011 at 9:48
  • Your code works fine for me. (provided nick variable contains "nick"). Commented Oct 13, 2011 at 9:54

3 Answers 3

1

I think this what you want

list.Where(i => i[0] == "nick")

It will return a IEnumerable<string[]> where nick if the first element in each string[]

Sign up to request clarification or add additional context in comments.

1 Comment

This also works - though my original code turned out to be okay, displaying it elsewhere was the issue!
0
 list.Where(x => x[0].Equals(nick)); 

1 Comment

@Gilad power of suggestion in question ;)
0

I guess you're after:-

list.Find(i => i.Equals("nick"))

I'm also guessing that this isn't what you mean....

Perhaps you have something more like this:-

    static void Main(string[] args)
    {
        var test = new List<string[]>() {
                                            new String[3] { "a", "b", "b" }, 
                                            new String[3] { "a", "c", "c" }, 
                                            new String[3] { "b", "b", "c" }, 
                                            new String[3] { "a", "d", "d" }, 
                                            new String[3] { "x", "y", "z" } 
                                        };
        var foundFirst = test.Find(i => i[0].Equals("a"));
        var foundAll = test.Where(i => i[0].Equals("a"));
    }

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.