Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

distinct values using LINQ [duplicate]

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?

like image 536
SUPER_USER Avatar asked Sep 10 '26 14:09

SUPER_USER


1 Answers

You can add a GroupBy

var list =  from ta in tempList
            group ta by ta.Region into g
            select g.FirstOrDefault();
like image 81
Kevin Wallis Avatar answered Sep 13 '26 15:09

Kevin Wallis