1

I am trying to get a list of a list of strings in the code below and I am getting an on select that reads:

Cannot implicitly convert type 'System.Data.EnumerableRowCollection>' to 'System.Collections.Generic.List>'

List<List<string>> rows = (from myRow in data.AsEnumerable()
                            select new List<string> {myRow["FirstName"].ToString(),
                                myRow["LastName"].ToString(),
                                myRow["Department"].ToString(),
                                myRow["Birthdate"].ToString(),
                                myRow["Description"].ToString()
                            });

How can I get a list of a list of strings?

2 Answers 2

5

Linq is working with enumerables (IEnumerable). You need to convert to a list:

List<List<string>> rows = (from myRow in data.AsEnumerable()
                            select new List<string> {myRow["FirstName"].ToString(),
                                myRow["LastName"].ToString(),
                                myRow["Department"].ToString(),
                                myRow["Birthdate"].ToString(),
                                myRow["Description"].ToString()
                            }).ToList();
Sign up to request clarification or add additional context in comments.

2 Comments

Thanks this works. I actually tried this, but my stupid VS IDE didn't recognize it. Once I clicked Build, the IDE recognized it.
You're welcome. Even better than "Thanks": Tick the checkmark! :-)
2

Method syntax is more concise:

List<List<string>> rows = data.AsEnumerable()
    .Select(r => r.ItemArray.Select(o => o + "").ToList())
    .ToList();

"half" query syntax:

rows = (from row in data.AsEnumerable()
        select row.ItemArray.Select(o => o + "").ToList())
       .ToList();

1 Comment

Shorter yes. But clearer what the result is? Depends. But I like the use of ItemArray.

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.