-1

I'm trying to get all the info (column, rows) from a table in SQL and send it as a model to the view.

I also want it to be Distinct.

My controller:

        MVCEntities me = new MVCEntities();
        List<CarsCategory> arr = me.CarsCategories.ToList();
        return View(arr);

My view model:

@model IEnumerable<CarsCategory>

In the view I'm trying to loop through a certain column like this:

    <select id="SelectManufacturer">

        @foreach (var i in Model)
        {
            <option value="@i.Manufacturer">@i.Manufacturer</option>
        }
    </select>

How do I make it Distinct? When I try to add Distinct it gives me system.linq.enumerable+<DistinctIterator> ..

Cœur
  • 37,241
  • 25
  • 195
  • 267
  • 1
    please share the exact error details and where did you try distinct? have you tried me.CarsCategories.Distinct().ToList(); – Balaji Marimuthu Jan 05 '17 at 17:41
  • @BalajiM It's not an error. When I loop through i.manufacturer without the distinct (Like the example above) it gives me the same name a few times, and when I add distinct to i.manufacturer like this (i.manufacturer.distinct()) it gives me "system.linq.enumerable+" instead of the real names – MeMyselfAndi Jan 05 '17 at 17:47
  • 1
    Use distinct at controller not view List arr = me.CarsCategories.Distinct().ToList(); – Balaji Marimuthu Jan 05 '17 at 17:49
  • If you just need distinct Names [see this answer](http://stackoverflow.com/a/28112531/2030565) and filter this in your controller before returning to the view. – Jasen Jan 05 '17 at 17:51
  • @BalajiM Still not working. already tried that. still gives the same name over and over – MeMyselfAndi Jan 05 '17 at 17:51
  • me.CarsCategories.Select(s=>s.Manufacturer).Distinct().ToList() or use groupby – Balaji Marimuthu Jan 05 '17 at 17:55
  • @Jasen no it's not good, it forces me to choose a column. I want the whole table – MeMyselfAndi Jan 05 '17 at 17:58
  • @BalajiM I need the model to contain the whole table. when I use lambda expression it forces me to choose a column – MeMyselfAndi Jan 05 '17 at 18:00
  • choose the column whatever you want – Balaji Marimuthu Jan 05 '17 at 18:07
  • @BalajiM Man, I'm working in my view with more than just one Select tag! I have a few more Select tags that I want to create and they need to be based on another columns from the table, I hope you understand me – MeMyselfAndi Jan 05 '17 at 18:09

2 Answers2

1

Although it's not a good approach to process data inside the view, your solution might look like this:

<select id="SelectManufacturer">
    @{
        var manufacturers = Model.Select(x => x.Manufacturer).Distinct().ToList();

        foreach (var i in manufacturers)
        {
            <option value="@i">@i</option>
        }
    }
</select>
dodbrian
  • 1,394
  • 14
  • 18
1

The controller should be responsible to supply the View with the data, the view should not be polutted with a bunch of logic to try to aggregate this data unless you want unmaintainable code. The best approach is to extend your view model to have multiple properties.

Models

public class CategoryModel{
    public List<CarsCategory> CarCategories {get;set;}
    public List<Manufacturer> Manufacturers {get;set;}
}

public class Manufacturer{
    public int Id {get;set;}
    public string Name {get;set;}
}

Controller code

// you need to ensure that if you are using EF the context is disposed after you are done using it!
using(MVCEntities me = new MVCEntities()) {
  var model = new CategoryModel();
  model.CarCategories = me.CarsCategories.ToList();
  // you need to supply the correct Id and Name locations in your model as you did not share this
  model.Manufacturers = model.CarCategories.Select(x => new Manufacturer(){Id = x.prop.id, Name = x.prop.name}).Distinct();
  return View(model);
}

Razor View

@model CategoryModel

<select id="SelectManufacturer">

    @foreach (var i in Model.Manufacturers)
    {
        <option value="@i.Id">@i.Name</option>
    }
</select>
Igor
  • 60,821
  • 10
  • 100
  • 175