Get Distinct value using Linq in ASP.Net MVC

Last Reply 14 days ago By dharmendr

Posted 18 days ago

Hi,

I have the list here

        private List<Product> GetProducts() {  
            List<Product> products = new List<Product>();
            products.Add(new Product { ProductID = 1, Make = "Samsung", Model = "Galaxy S3" });
            products.Add(new Product { ProductID = 2, Make = "Samsung", Model = "Galaxy S4" });
            products.Add(new Product { ProductID = 3, Make = "Samsung", Model = "Galaxy S5" });
            products.Add(new Product { ProductID = 4, Make = "Apple", Model = "iPhone 5" });
            products.Add(new Product { ProductID = 5, Make = "Apple", Model = "iPhone 6" });
            products.Add(new Product { ProductID = 6, Make = "Apple", Model = "iPhone 6" });
            products.Add(new Product { ProductID = 7, Make = "HTC", Model = "Sensation" });
            products.Add(new Product { ProductID = 8, Make = "HTC", Model = "Desire" });
            products.Add(new Product { ProductID = 9, Make = "HTC", Model = "Desire" });
            products.Add(new Product { ProductID = 10, Make = "Nokia", Model = "Lumia 735" });
            products.Add(new Product { ProductID = 11, Make = "Nokia", Model = "Lumia 930" });
            products.Add(new Product { ProductID = 12, Make = "Nokia", Model = "Lumia 930" });
            products.Add(new Product { ProductID = 13, Make = "Sony", Model = "Xperia Z3" });

            return products;
        }

out of this list I need to get the unique "MAKE" using LAMBA or LINQ and assign to a dropdown MVC (Model.List)

public List<Mobilevar> List { get; set; }

 

public class Mobilevar
{
    public int Id { get; set; }
    public string type { get; set; }
} 

 

@Html.DropDownListFor(o => o.MOBList.MID, new SelectList(Model.List, "Id", "type", Model.ReqList.SelectMID), new { @class = "form-control" })

 

 

Posted 14 days ago

It is not possible as per your data.

For samsung there are 3 ProductID 1,2,3.

So if you apply distinct then suppose 1st one you get then you will always get the ProductID 1.

You never got the ProductID 2 and 3.