Using Distinct in Lambda Expression and use it in

2019-09-11 10:19发布

Here, i am trying to first get the unique Main Category Name, under that the brands available in that Main category and under that the Sub categories of that particular Brand and Main Category. But distinct is not working and returning all values. Here is the code of model.

public MvcHtmlString Submenu()
        {
            var products = _db.Products;
            var sb = new StringBuilder();

                sb.Append("<ul>");
                var mainmenu = products.Select(p => p.MainCategory).Distinct();
            if(mainmenu!= null)
            {
                int i = 0;
                foreach (var prods in products)
                {
                    i++;
                    sb.AppendFormat("<li><a  class=\"main-link\" href=\"#\">{0}</a>\n", prods.MainCategory);
                    var brans = products.Where(p => p.MainCategory == prods.MainCategory).Select(p => p.Brand).Distinct();
                    if (brans != null)
                    {
                        int j = 0;
                        foreach (var prodss in products)
                        {
                            j++;
                            sb.Append("<ul>");
                            sb.AppendFormat("<li><a  class=\"main-link\" href=\"#\">{0}</a>\n", prodss.Brand);
                            //var subCats = _db.Products.SqlQuery("Select Distinct(SubCategory) from Product where MainCategory = '" + mainmenu + "' && Brand = '"+brans+"'");
                            var subCats = products.Where(p => p.MainCategory == prods.MainCategory && p.Brand == prodss.Brand).Select(p => p.SubCategory).Distinct();
                            if (subCats != null)
                            {
                                int k = 0;
                                foreach (var pods in products)
                                {
                                    k++;
                                    sb.Append("<ul>");
                                    //sb.AppendFormat("<li><a   href=\"#\">{0}</a></li>\n", pods.SubCategory);
                                    sb.Append("</ul>");
                                }
                            }
                            sb.Append("</li>");
                            sb.Append("</ul>");
                        }
                    }
                }
                sb.Append("</li>");
            }
            sb.Append("</ul>");
            return new MvcHtmlString(sb.ToString());
        }

Please guide me

1条回答
萌系小妹纸
2楼-- · 2019-09-11 10:56

You need to override the Equals and GetHashCode for your Product class but i think you just want to distinct by the name or id of product,so you can try this extenssion method:

public static IEnumerable<TSource> DistinctBy<TSource, TKey>(this IEnumerable<TSource> that, Func<TSource, TKey> selector)
{
    var set = new HashSet<TKey>();
    foreach (var element in that)
    {
        if (set.Add(selector(element)))
        {
            yield return element;
        }
    }
}
查看更多
登录 后发表回答