Click here to Skip to main content
15,908,254 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I try linq query

<asp:DropDownList ID="regiondrop" runat="server" AutoPostBack="True"
onselectedindexchanged="regiondrop_SelectedIndexChanged">


I try to show distinct values in drop-down on page load. I try this query to get

What I have tried:

values

T1 tea = new T1();
var list1 = tea.t1.ToList();
var list = (from ta in list1
orderby ta.Region
select new { ta.Region,ta.RegionID }).Distinct().ToList();
but this shows all same values where as i want distinct values


in my scenario data is

RID Region
1 Paris
2 Canada
3 UK
4 Paris
5 Canada
6 London
so in this Canada and Paris 2 times so i want this 1 time in drop-down

any solution?
Posted
Updated 21-Jun-16 21:47pm
Comments
Karthik_Mahalingam 22-Jun-16 1:33am    
It is already distinct only [Id and region combination]
which one you will ignore. [ 1 Paris , 2 Canada ] or [4 Paris , 5 Canada ]
distinct() will not work in this way.

Try Grouping them:
C#
List<T1> list1 = new List<T1>();
list1.Add(new T1 { RegionID = 1, Region = "Paris" });
list1.Add(new T1 { RegionID = 2, Region = "Canada" });
list1.Add(new T1 { RegionID = 3, Region = "UK" });
list1.Add(new T1 { RegionID = 4, Region = "Paris" });
list1.Add(new T1 { RegionID = 5, Region = "Canada" });
list1.Add(new T1 { RegionID = 6, Region = "London" });
var list = from ta in list1
            group ta by ta.Region into newgroup
            orderby newgroup.Key
            select newgroup;
foreach (var t in list)
    {
    T1 t1 = t.First();
    Console.WriteLine("{0}:{1}", t1.RegionID, t1.Region);
    }
 
Share this answer
 
Comments
super_user 22-Jun-16 2:11am    
when i try this .
protected void Page_Load(object sender, EventArgs e)
{

T1 tea = new T1();
var list1 = tea.table1.ToList();
var list = from ta in list1
group ta by ta.Region into newgroup
orderby newgroup.Key
select newgroup;

this shows same output like repeated values in dropdown



if(!Page.IsPostBack)
{
regiondrop.DataSource = list;
regiondrop.DataTextField = "Region";
regiondrop.DataValueField = "RID";
regiondrop.DataBind();

}

}
OriginalGriff 22-Jun-16 2:48am    
Yes...that's why my code has the call to First in the foreach...
If you want to use the list directly, change the select:

select newgroup.First();
try this

C#
var list1 = tea.t1.ToList();
     Dictionary<int, string> dict = new Dictionary<int, string>();
     foreach (var item in list1.OrderBy(k=>k.Region))
     {
         if(!dict.ContainsValue(item.Region))
         dict.Add(item.RegionID, item.Region);
     }
     var output = dict.Select(k => new { RegionId = k.Key, Region = k.Value }).ToList();

     egiondrop.DataSource = output;
     regiondrop.DataTextField = "Region";
     regiondrop.DataValueField = "RegionId";
     regiondrop.DataBind();
 
Share this answer
 
Comments
super_user 22-Jun-16 2:21am    
this shows same output like repeated values in dropdown
Karthik_Mahalingam 22-Jun-16 2:23am    
post the code.
super_user 22-Jun-16 2:31am    
protected void Page_Load(object sender, EventArgs e)
{
T1 tea = new T1();
var list1 = tea.t1.ToList();
Dictionary<int, string=""> dict = new Dictionary<int, string="">();
foreach (var item in list1.OrderBy(k => k.Region))
{
if (!dict.ContainsValue(item.Region))
dict.Add(item.RegionID, item.Region);
}
var output = dict.Select(k => new { RegionId = k.Key, Region = k.Value }).ToList();




if(!Page.IsPostBack)
{
regiondrop.DataSource = list;
regiondrop.DataTextField = "Region";
regiondrop.DataValueField = "RegionID";
regiondrop.DataBind();

}

}
Karthik_Mahalingam 22-Jun-16 2:34am    
if(!Page.IsPostBack)
{
regiondrop.DataSource = output
Maciej Los 22-Jun-16 3:05am    
5ed!
Lambda solution:

C#
T1 tea = new T1();
var list = tea.t1.GroupBy(x=>x.Region)
                 .Select(grp=>new
                   {
                     RegionId = grp.First(),
                     Region = grp.Key
                   })
                 .ToList();
//further
regiondrop.DataSource = list;
 
Share this answer
 

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