1

So I was working on a DropDown list and it works, but it has many duplicate emails, I just want to see distinct emails... So I tried doing .Distinct() in the model class, but that still gave me duplicates..

Model Class:

 public string One_Manager_Email{ get; set; }

 public List<Hello> getManagers()
        {
            var que = (from wr in db.View
                       select new Hello
                       {
                           O_Manager_Email= wr.One_Manager_Email
                       }).Distinct().ToList();
            return que;
        }

Controller Class: (This is probably where the problem is happening)

    public ActionResult Index()
        {
            test = new Hello();
            ViewBag.Managers = new SelectList(test.getManagers(), "", "O_Manager_Email");
            return View(test.getStuff());
         }

View Class:

<div>
    @Html.DropDownList("Managers", ViewBag.Managers as SelectList)
</div>

Any help would be great, thank you!

Peter
  • 65
  • 2
  • 13
  • You can use a `.GroupBy()` or implement an `IEqualityComparer` - refer [Various Ways to Get Distinct Values from a List using LINQ](http://vmsdurano.com/various-ways-to-get-distinct-values-from-a-listt-using-linq/) –  Oct 25 '18 at 20:58

3 Answers3

1

You need to group your objects by the property you want them to be distinct by, and then select the first element of the grouping.

 public List<Hello> getManagers()
        {
            var que = (from wr in db.View
                       select new Hello
                       {
                           O_Manager_Email= wr.One_Manager_Email
                       })
                      .GroupBy(g => g.O_Manager_Email) //group by property
                      .Select(g => g.First()) //take first element from every grouping
                      .ToList();
            return que;
        }

For some more details, you can see this post that has more details on grouping and distinct: LINQ's Distinct() on a particular property

Joe Brunscheon
  • 1,949
  • 20
  • 21
0

Distinct won't work on objects like that, as objects are always distinct, but you can try using group by in your query, something along these lines:

var que = (from wr in db.View
           group new {wr.One_Manager_Email}
           by new {wr.One_Manager_Email} into grouped
           select new Hello
           {
               O_Manager_Email= grouped.Key.One_Manager_Email
           }).ToList();
return que;
Felipe Martins
  • 184
  • 2
  • 12
0

If you just need email address, you can select just string instead of selecting Hello object. If you select Hello object and try to distinct you like that way, you obviously get duplicated items. Because every object is already unique.

I believe you have already answer. GroupBy might solve your problem. However unless you really need GroupBy, don't use GroupBy! It's really expensive operation.

In your case, you should use DistinctBy.

var distinctList = list.DistinctBy(x => x.Prop).ToList();

On your code:

        var que = (from wr in db.View
                   select new Hello
                   {
                       O_Manager_Email = wr.One_Manager_Email
                   }).DistinctBy(x=>x.O_Manager_Email).ToList();

Oh, in terms of DistinctBy usage, you should import namespace of Microsoft.Ajax.Utilities.

using Microsoft.Ajax.Utilities;
Mehmet Taha Meral
  • 3,315
  • 28
  • 30