日期:2014-05-20  浏览次数:21051 次

LINQ去重
C# code
using (var ctx = new ReadModel.ReadEntities())
            {
                var pwList = new List<ReadModel.product_whole>();
                try
                {
                    pwList =
                        ctx.product_whole.Where(
                            p =>
                            (p.p_code != null && p.p_code.IndexOf(strKeyWords) > -1) ||
                            (p.p_name != null && p.p_name.IndexOf(strKeyWords) > -1) ||
                            (p.commonName != null && p.commonName.IndexOf(strKeyWords) > -1) ||
                            (p.enterprise != null && p.enterprise.IndexOf(strKeyWords) > -1) ||
                            (p.brand != null && p.brand.IndexOf(strKeyWords) > -1)|| 
                            (p.keywords != null && p.keywords.IndexOf(strKeyWords) > -1) ||
                            (p.spell != null && p.spell.IndexOf(strKeyWords) > -1) ||
                            (p.spec != null && p.spec.IndexOf(strKeyWords) > -1) ||
                            (p.type_name_tree != null && p.type_name_tree.IndexOf(strKeyWords) > -1) ||
                            (p.manufacturer_brand != null && p.manufacturer_brand.IndexOf(strKeyWords) > -1) ||
                            (p.attr_value != null && p.attr_value.IndexOf(strKeyWords) > -1) ||
                            (p.secondType != null && p.secondType.IndexOf(strKeyWords) > -1) ||
                            (p.websiteSearchKeyWords != null && p.websiteSearchKeyWords.IndexOf(strKeyWords) > -1)).ToList();
                }
}


这个LINQ查询出来的结果里面好多重复数据,如何去重啊?


------解决方案--------------------
pwList=pwList.Distinct();
------解决方案--------------------
public class ProductComparer : IEqualityComparer<ReadModel.product_whole>
{
public bool Equals(ReadModel.product_whole t1, ReadModel.product_whole t2)
{
return t1.ID==t2.ID && t1.Name==t2.Name;
}
public int GetHashCode(ReadModel.product_whole t)
{
return t.ToString().GetHashCode();
}
}


pwList=pwList.Distinct(new ProductComparer ());
------解决方案--------------------
学习了
pwList=pwList.Distinct();
pwList=pwList.Distinct(new ProductComparer ());
这两者有什么区别吗?