Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Cannot get distinct values using a SelectListitem

Tags:

c#

linq

I'm trying to get distinct values for a dropdownlist, but when using the following syntax, it brings me back all the rows.

Can someone please inform me how to get a distinct value set with the correct syntax?

IEnumerable<SelectListItem> ldidList = _db.TrafficHits.Select(c => new SelectListItem
            {
                Value = c.Id.ToString(),
                Text = c.ldid
            }).Distinct();
like image 557
sagesky36 Avatar asked May 01 '15 14:05

sagesky36


People also ask

How to get distinct values for a specific field in list?

If you want to achieve the distinct values for a specific field in the list, you can use the following two methods: 1 Using GroupBy and Select functions#N#In this approach, you need to use two LINQ functions i.e. GroupBy and Select to... 2 Using Select and Distinct functions More ...

What is the use of SELECT DISTINCT in SQL?

The SELECT DISTINCT statement is used to return only distinct (different) values. Inside a table, a column often contains many duplicate values; and sometimes you only want to list the different (distinct) values. SELECT DISTINCT column1, column2, ...

Does distinct use the default comparer for selectlistitem?

The problem with your current code is that Distinct will use the default comparer for SelectListItem. You will need to provide a custom comparer like this:-

How do you make a distinct list in LINQ?

LINQ DistinctBy () on a Property If you need a distinct list based on one or more properties, you can the following code: List bookList = GetBooks().DistinctBy(book => new { book.BookID, book.Title }); Using GroupBy and Select on a Property


2 Answers

The problem with your current code is that Distinct will use the default comparer for SelectListItem. You will need to provide a custom comparer like this:-

public class SelectListItemComparer : IEqualityComparer<SelectListItem>
    {
        public bool Equals(SelectListItem x, SelectListItem y)
        {
            return x.Text == y.Text && x.Value == y.Value;
        }

        public int GetHashCode(SelectListItem  item)
        {
            int hashText = item.Text == null ? 0 : item.Text.GetHashCode();
            int hashValue = item.Value == null ? 0 : item.Value.GetHashCode();
            return hashText ^ hashValue;
        }
    }

Then you can use it like this:-

IEnumerable<SelectListItem> ldidList = _db.TrafficHits.Select(c => new SelectListItem
            {
                Value = c.Id.ToString(),
                Text = c.ldid
            }).Distinct(new SelectListItemComparer());
like image 78
Rahul Singh Avatar answered Sep 18 '22 21:09

Rahul Singh


You can use the group by, then you select the first element per group :

IEnumerable<SelectListItem> ldidList = _db.TrafficHits
                                          .GroupBy(t => t.Id)
                                          .Select(g => g.First())
                                          .Select(c => new SelectListItem
                                          {
                                             Value = c.Id.ToString(),
                                             Text = c.ldid
                                         });
like image 20
Bilel Chaouadi Avatar answered Sep 18 '22 21:09

Bilel Chaouadi