Click here to Skip to main content
15,885,216 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
Hi all,i having a code like this and iam binding that string value to dropdownlist.

string test = ds.Tables[0].AsEnumerable().Where(x => x.Field<int>("Id") == lbl.Text).Select(z => z.Field<string>("Description")).ToString();

Drpdwn.Items.Add(test);

but when execution it binds like this,

System.Data.EnumerableRowCollection'1[System.String]

How to fix this?Thanks in Advance..
Posted
Updated 13-Nov-15 19:52pm
v2

1 solution

Loops through the collection and add it to the dropdown items.
C#
var items = ds.Tables[0].AsEnumerable().Where(x => x.Field("Id") == lbl.Text).Select(z => z.Field("Description")).ToList();

foreach(var i in items)
{
    Drpdwn.Items.Add(i.ToString());
}


-KR
 
Share this answer
 
Comments
Maniraj.M 12-Nov-15 2:08am    
The idea is good but you forget to specify the Type of the Field so it showing compile time error..Is there any possibilities?
Krunal Rohit 12-Nov-15 2:30am    
Debug your code and check what are you getting in items.
Post the error message here.

-KR
Matt T Heffron 12-Nov-15 16:40pm    
If you're just going to iterate over the result of the Linq statement then the .ToList() is unnecessary.
Maniraj.M 13-Nov-15 1:29am    
Then what's the correct one?
Matt T Heffron 13-Nov-15 12:35pm    
Just omit the .ToList(). It should behave exactly the same without needing to allocate a List that will be dropped on the floor right after the foreach.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900