3

I try to get distinct values in LINQ i try this for this first i create method and then i call this method on page load and assign

regiondrop.DataSource = getregion();
regiondrop.DataSourc=DataTextField="Region"
regiondrop.DataSourc==DataTextField="RID"


 private List<tab1> getregion()
        {
            using (T1 tee = new T1())
            {
            var tempList = tee.tbl1.ToList();
            var list = (from ta in tempList
            select new { ta.Region, ta.RID }).Select(x => new tbl1
            {
             Id = x.RID,
             reg=x.Region
             }).ToList();
            return list;
            }

        }

Data in db like this

RID Region
1   Canada
2   UK
3  London
4  Paris
5  UK
6  Brazil
7  London

Data in drop-down like this

Canada
UK
London
Paris
UK
Brazil
London

but i want data like this

Canada
UK
London
Paris
Brazil

any solution?

SUPER_USER
  • 275
  • 3
  • 16
  • You could use `List.Distinct()` with a custom comparator on `Region` property. ([link](https://msdn.microsoft.com/en-us/library/bb920306%28v=vs.90%29.aspx)) – user5226582 Jun 22 '16 at 06:47

1 Answers1

2

You can add a GroupBy

var list =  from ta in tempList
            group ta by ta.Region into g
            select g.FirstOrDefault();
wake-0
  • 3,918
  • 5
  • 28
  • 45