1

I have two data tables in my wcf service method. I want to add all the rows of those data tables in a list using c#.

I am using foreach loop to add all the rows of a data table. I can't figure out a way to do something like this,

foreach (DataRow dr in dt.Rows && DataRow dr1 in dt1.Rows)
{       
    lst.Add(
           new GetData{week = dr["Week_Number"].ToString()
           , Total_Created = dr["Total_Created"].ToString()
           , Total_Open = **dr1**["Total_Open"].ToString()}
           );

}

Alexei Levenkov
  • 98,904
  • 14
  • 127
  • 179
Arpita
  • 445
  • 3
  • 14
  • 28
  • So your dt has Week_Number and Total_Created columns, and dt1 has Total_Open column, and you want to combine the three columns into one List? You can loop through them separately, add the dt rows into the List, then loop through the dt1 rows and add the values of Total_Open to the existing List. – dub stylee Nov 26 '14 at 19:51
  • Side note: join on source tables may be better approach. – Alexei Levenkov Nov 26 '14 at 19:53

3 Answers3

5

I'm assuming that your dr1's rows are supposed to align with dr's rows.

This doesn't involve the && operator at all, but rather using a LINQ method to combine the values together:

var data = dt.Rows.Cast<DataRow>()
      .Zip(dt1.Rows.Cast<DataRow>(),
          (dr, dr1) => new GetData{
               week = dr["Week_Number"].ToString()
               , Total_Created = dr["Total_Created"].ToString()
               , Total_Open = dr1["Total_Open"].ToString());
lst.AddRange(data);
StriplingWarrior
  • 151,543
  • 27
  • 246
  • 315
4

As you probably discovered, you can't do this exactly in C#. For starters, it's not clear to me what you want to happen, as there are at least three obvious interpretations for your pseudo-code:

  1. Produce a cartesian product - act on every possible combination of dr and dr1. To get this effect, you can use nested loops, e.g.:

    foreach (DataRow dr in dt.Rows)
    {
      foreach (DataRow dr1 in dt1.Rows)
      {
         // do stuff.
      }
    }
    
  2. Operate on sequential pairs, e.g. dt row one and dt1 row one. For this, you can use the LINQ Zip feature to combine pairs of elements:

    dt.Rows.Cast<DataRow>().Zip(dt1.Rows.Cast<DataRow()),
     (dr, dr1) => 
     {
       // do stuff
     };
    
  3. Operate on matching pairs, e.g. there is a dt1 row that is somehow associated with each dt row. For this, you'll need to just loop through the first table and "look up" the matching row in the second, using whichever criteria makes sense:

    foreach (DataRow dr in dt.Rows)
    {
       DataRow dr1 = dt1.Select(someQueryHere)[0];
       // do stuff
    }
    
Michael Edenfield
  • 28,070
  • 4
  • 86
  • 117
  • Much better way of putting what I was trying to say. +1 :) – BradleyDotNET Nov 26 '14 at 19:54
  • 1
    While I believe the question itself is exact [duplicate](http://stackoverflow.com/questions/1955766/iterate-two-lists-or-arrays-with-one-foreach-statement-in-c-sharp), this explanation should help OP to clarify what actually needed. – Alexei Levenkov Nov 26 '14 at 19:56
  • 1
    @Alexei I almost left a comment asking for clarification, but I figure this way, anyone searching for this kind of multi-foreach for any of those reasons will find a good answer :) – Michael Edenfield Nov 26 '14 at 19:58
0

Concat with LINQ:

var dr1 = dt.Rows.Cast<DataRow>();
var dr2 = dt1.Rows.Cast<DataRow>();
var both = dr1.Concat(dr2);

foreach (DataRow dr in both)
{
}
lante
  • 7,192
  • 4
  • 37
  • 57