Click here to Skip to main content
15,878,871 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I've two dropdownlists. I'm retrieving two columns named(Group & SubGroup) from database and filling in to Dataset.

Assume:
Group:
A
B
C

SubGroup:
Belongs to A group
1
2
Belongs to B group
3
4

Belongs to C group
5
6


I copied Dataset Column named "Group" into the Dropdownlist1.

C#
//Get Group Data
          var Y = (from X in dsOptionDetails.Tables[0].AsEnumerable()
                   select X["Group"]).Distinct().ToList();

          foreach (var item in Y)
          {
              ddlModulegroup.Items.Add(item.ToString());
          }



Now i want to write the Linq query to get the the Dropdownlist1 Selected item respective Child items into DropDownlist2. I'm new to linq, May i knw how?


SQL
//Get Subgroup Data
var Z = (from X in ds.Tables[0].AsEnumerable()
                 where X["Group"]= ddlModulegroup.SelectedItem.ToString()
                 select X["SubGroup"]).ToList();

        foreach (var item in ddlModulegroup.SelectedItem.ToString())
        {

            ddlModuleSubgroup.Items.Add(item.ToString());
        }
Posted
Updated 9-Sep-15 2:15am
v2
Comments
[no name] 9-Sep-15 9:01am    
Two tables Group and SubGroup are present in Dataset?
If yes what is design of two table?
JanardhanSharma 9-Sep-15 9:45am    
Yes, both the columns are in same dataset. And these Two columns Belongs to same table. Group and SubGroup are Column names

1 solution

If i understand you correctly...

Try this:
C#
DataTable dt = new DataTable();
dt.Columns.Add(new DataColumn("Group", typeof(string)));
dt.Columns.Add(new DataColumn("SubGroup", typeof(int)));

dt.Rows.Add(new object[]{"A", 1});
dt.Rows.Add(new object[]{"A", 2});
dt.Rows.Add(new object[]{"B", 3});
dt.Rows.Add(new object[]{"B", 4});
dt.Rows.Add(new object[]{"C", 5});
dt.Rows.Add(new object[]{"C", 6});

string dropdownselecteditem = "A"; //get value from DropDown

var result = dt.AsEnumerable()
    .Where(x=>x.Field<string>("Group")==dropdownselecteditem);

foreach(var record in result)
{
    Console.WriteLine("{0}", record.Field<int>("SubGroup"));
}


Result:
1
2
 
Share this answer
 
Comments
Wendelius 13-Sep-15 1:19am    
Looks good.
Maciej Los 13-Sep-15 16:18pm    
Thank you, Mika

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