本文介绍了在Lambda表达式中使用Distinct并在foreach循环中使用它的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在这里,我试图首先获得唯一的主要类别名称,即该主要类别中可用的品牌以及该特定品牌和主要类别的子类别下的名称.但与众不同的是无法正常工作并返回所有值.这是模型的代码.

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());
        }

请指导我

推荐答案

您需要为您的Product类覆盖Equals和GetHashCode,但是我认为您只想按产品名称或ID进行区分,因此您可以尝试一下延伸方法:

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;
        }
    }
}

这篇关于在Lambda表达式中使用Distinct并在foreach循环中使用它的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-16 04:40