1

I am trying to put a MySQL select into a List. I found one piece of code that works, however I need to add more than one column. I want to return an array of all the data in the select.

List<string> list = (from IDataRecord r in dataReader
                     select (string)r["FieldName"]
                    ).ToList();

Code was found here: Fill an array (or arraylist) from SqlDataReader

1 Answer 1

2

This should do it:

var list = (from IDataRecord r in dataReader
            select new 
                   {
                        Field1 = (string)r["Field1"],
                        Field2 = (int)r["Field2"],
                         ...
                   }
            ).ToList();

Just add fields as needed.

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

2 Comments

It seems I have having an issue with that, I was unable to use var list, so I tried List<String> and sense the data types are not all String it throws a fit. Im guessing i need to do something such as IEnumerable, but im not sure how.. The exactly error I get with the List<String> is Cannot implicitly convert type 'System.Collections.Generic.List<AnonymousType#1>' to 'System.Collections.Generic.List<string>' it doesn't like the .ToList()
I got it figured out now.. Thanks!

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.