0

I try linq query

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

I try to show distinct values in drop-down on page load. I try this query to get 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

update

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?

SUPER_USER
  • 275
  • 3
  • 16

2 Answers2

1

Check this

var list1 = tea.t1
            .GroupBy(i => i.Region)
            .Select(i => new 
            {
                RegionID = i.Min(t => t.RegionID) // Or Max what you want
                Region = i.Key,                 
            })
            .OrderBy(i => i.Region).ToList();
neer
  • 4,031
  • 6
  • 20
  • 34
0

If you want to get values distinct by a property -like Region- you can use GroupBy function.

T1 tea = new T1();
var list1 = tea.t1.ToList();
var list = (from ta in list1
        orderby ta.Region
        select new { ta.Region, ta.RegionID })
        .GroupBy(g=>g.Region)
        .Select(g=>g.First())
        .ToList();

For an extension method : LINQ: Distinct values

Community
  • 1
  • 1
Cihan Yakar
  • 2,402
  • 28
  • 30
  • in my scenario.. data is .. `RID Region 1 Paris 2 Canda 3 UK 4 Paris 5 Canda 6 London ` now in this canada and paris appear 2 times so i want this 1 time in dropdown – SUPER_USER Jun 22 '16 at 04:23
  • this solution not work . when i try this data displayed in dropdown multiple times ...check update please – SUPER_USER Jun 22 '16 at 04:25
  • Then group by `Region` instead `RegionId`. I edited sample code. – Cihan Yakar Jun 22 '16 at 11:35