1

I'm receiving an exception like cannot convert from 'List' to 'string' when i'm declaring a list.

 var query = new List<string>(){
                    new List<string>{"a", "b","c"},
                    new List<string> {"a"}};

I couldn't find out the error in above.Can somebody help me in this?

3 Answers 3

5
var query = new List<List<string>>(){
                    new List<string>{"a", "b","c"},
                    new List<string> {"a"}};
Sign up to request clarification or add additional context in comments.

3 Comments

This is correct. Basically we are declaring a List which holds list. And that List holds string datatype.
@KaipaMSarma: To be explicit, we're not declaring a list of list, we're declaring a list of list of string. If you try to add a list of int to your outer list, you'd get an error at design time.
I mean the same, Sorry if my explanation is bad ;(
2

You can't pass a list as string, you should do as bellow if you want a list of list of string:

 var query = new List<List<string>>(){
                new List<string>{"a", "b","c"},
                new List<string> {"a"}};

or do as bellow if you want a list of string:

 var query = new List<string>(){"a", "b","c"};

Comments

1

If you want a List of List of strings, you can do what vc74 or Saeed suggest. If you want to end up with a single List containing all the strings from the two other, you can do :

var firstList = new List<string>{"a", "b","c"};
var secondList = new List<string>{"a", "b","c"};

var query = new List<string>();
query.AddRange(firstList);
query.AddRange(secondList);

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.