首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > .NET > .NET >

LINQ去重解决办法

2012-11-03 
LINQ去重C# codeusing (var ctx new ReadModel.ReadEntities()){var pwList new ListReadModel.produ

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 ());
这两者有什么区别吗?

热点排行