tags:

views:

123

answers:

4

Is there a way to take out the foreach in the following code in linq yet produce the same output?

    DropDownList ddl = new DropDownList();
    foreach (DataRow row in ds.Tables[0].Rows)
    {
        if ((byte)row["ListTypeID"] == 0)
        {
            item = new ListItem(row["ListText"].ToString(), string.Format("{0}:{1}", row["ListTypeID"].ToString(), row["ListID"].ToString()));
            ddl.Items.Add(item);
        }
        else
        {
            item = new ListItem(row["ListText"].ToString(), string.Format("{0}", row["ListID"].ToString()));
            ddl.Items.Add(item);
        }
    }
+3  A: 
ddl.Items.AddRange(ds.Tables[0].Rows.Select( row => 
(byte)row["ListTypeID"] == 0
  ? new ListItem(row["ListText"].ToString(), string.Format("{0}:{1}", row["ListTypeID"].ToString(), row["ListID"].ToString()))
  : new ListItem(row["ListText"].ToString(), string.Format("{0}", row["ListID"].ToString()))           
));

a bit of cleaning

ddl.Items.AddRange(ds.Tables[0].Rows.Select( row => 
(byte)row["ListTypeID"] == 0
  ? new ListItem(row["ListText"].ToString(), string.Format("{0}:{1}", row["ListTypeID"], row["ListID"]))
  : new ListItem(row["ListText"].ToString(), string.Format("{0}", row["ListID"]))           
));

leave foreach :)

Andrey
What a mess...^^
Daniel Brückner
changing foreach to select... not sure how better this one is...
Franci Penov
don't see much difference in your both suggestions
Teddy
thx for the answer, i wonder if the foreach is easier to read in this case.
Joo Park
-1 Where at all is this readable?
Peter Lillevold
i answered origianl question. readability was not main goal.
Andrey
+5  A: 

Instead of cramming all into a one-line lambda, I prefer readability over saving line space:

ddl.Items.AddRange(ds.Tables[0].Rows.Select( row => 
    {
        var listText = row["ListText"];
        var listTypeId = (byte)row["ListTypeID"];
        var listId = row["ListID"];

        var format = listTypeId == 0 ? "{0}:{1}" : "{1}";
        var itemText = String.Format(format, listTypeID, listId);

        return new ListItem(listText, itemText);
    }
    ));
Peter Lillevold
Nice approach..
Waleed A.K.
+1  A: 

You can use something like this

 var Items= (from DataRow dr in ds.Tables[0].AsEnumerable() 
        select new { 
        Text=dr.Field<string>("ListText"), 
        Value =dr.Field<byte>("ListTypeID") == 0?string.Format("{0}:{1}", dr.Field<string>("ListTypeID"), dr.Field<string>("ListID")):string.Format("{0}", dr.Field<string>("ListID"))
    }).ToList();

now you can use Items as data-source for the dropdown.

Waleed A.K.
+2  A: 

After a bit of reordering you can achieve the following - adding the items excluded.

var items = ds.Tables[0].Rows.Select(row =>
   new ListItem(row["ListText"].ToString(),
                String.Format(
                   (Byte)row["ListTypeID"] == 0 ? "{0}:{1}" : "{1}",
                   row["ListTypeID"], row["ListID"])));
Daniel Brückner
Better. You could save even more by dropping the last ToString()s, String.Format will take care of that.
Peter Lillevold
Of course...how could I overlook that...thx!
Daniel Brückner