3

Okay now this below is working

     List<string> lstKnownMoves = dsAttacksTemp.Tables[0].Rows.
Cast<DataRow>().Select(r => r["Column1"].ToString()).ToList();

But i want to make it work like below and not working

     List<string> lstKnownMoves = dsAttacksTemp.Tables[0].Rows.
Cast<DataRow>().Select(r => r["Column1"].ToString(),r => r["Column2"].ToString(),r => r["Column3"].ToString()).ToList();

So instead of 1 dataset datarow column, i want to add multiple columns into the list. How can i do that ?

4
  • You'd get a list of string arrays. Is that what you want? Presumably you'd like a list of an object with several string properties, no? Commented Dec 20, 2012 at 12:38
  • "not working" is not clear. What do you expect? What do you get instead? Commented Dec 20, 2012 at 12:39
  • Can you give an example with data? Commented Dec 20, 2012 at 12:39
  • check @SWeko answer. SelectMany is solving my problem. thanks a lot. Commented Dec 20, 2012 at 12:47

3 Answers 3

4

You could use SelectMany if you just to dump the contents of the fields into a list.

List<string> lstKnownMoves = dsAttacksTemp.Tables[0].Rows
 .Cast<DataRow>()
 .SelectMany(r => new string[]{
                    r["Column1"].ToString(),
                    r["Column2"].ToString(),
                    r["Column3"].ToString()})
 .ToList();

If however, you want to extract the fields into another object, you could do something like:

class KnownMove {
   public string Column1 {get; set;}
   public string Column2 {get; set;}
   public string Column3 {get; set;}
}

List<KnownMove > lstKnownMoves = dsAttacksTemp.Tables[0].Rows
 .Cast<DataRow>()
 .SelectMany(r => new KnownMove{
                    Column1 = r["Column1"].ToString(),
                    Column2 = r["Column2"].ToString(),
                    Column3 = r["Column3"].ToString()})
 .ToList();
Sign up to request clarification or add additional context in comments.

Comments

1

I assume you want a list of an object with several string properties instead of a list of string arrays.

List<object> lstKnownMoves = dsAttacksTemp.Tables[0].Rows.
Cast<DataRow>().Select(r => new { 
 Col1 = r["Column1"].ToString(), 
 Col2 = r["Column2"].ToString(), 
 Col3 = r["Column3"].ToString()
}).ToList();

This way you'd get a List<T> where T is an anonymous object with 3 string properties, namely Col1, Col2 and Col3. I guess this is what you are asking for.

3 Comments

thanks i tried this before asking but not working img707.imageshack.us/img707/5091/errorggn.png
it's not a List<string> it's a List<object>. Please see my updated answer.
ye but it is not solving my problem. object. check SWeko answer it is working great
1

Well, assuming you want to be able to access each column individually, you'd want to return an array of strings.

 List<string[]> lstKnownMoves = dsAttacksTemp.Tables[0].Rows
                          .Cast<DataRow>().Select(r => new[] 
                               { 
                                  r["Column1"].ToString(),  
                                  r["Column2"].ToString(), 
                                  r["Column3"].ToString() 
                               }).ToList();

Then for each row, you can get the string value of a column by index:

 foreach(var item in lstKnownMoves) 
      Console.WriteLine("Column1: {0} Column2: {1} Column3: {2}", item);

If you want basically a map of columns, and would rather not create a class to act as a tuple as in the selected answer, you can use List<dynamic>:

 List<dynamic> lstKnownMoves = dsAttacksTemp.Tables[0].Rows
                          .Cast<DataRow>().Select(r => new 
                               { 
                                  Column1 = r["Column1"].ToString(),  
                                  Column2 = r["Column2"].ToString(), 
                                  Column3 = r["Column3"].ToString() 
                               }).ToList<dynamic>();

 foreach(var item in lstKnownMoves) 
      Console.WriteLine("Column1: {0} Column2: {1} Column3: {2}", 
          item.Column1, item.Column2, item.Column3);

2 Comments

no i don't want to access each individually. i want to add those 3 columns to the list. thanks.
This does add the three columns to the list. It's not clear what you are asking. You're asking for a List<string>, but unless you just string concat all three columns (which seems to me to be entirely useless), you can't get at each column for each row unless you change the list element from string to string[] (or similar).

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.