如何判断循环集合里面的重复项

lizlqq5 2012-02-01 09:48:41
IList<VIWWHUSERDUTY> list = manager.GetAllVIWWHUSERDUTY(user.PKID.ToString());
for (int i = 0; i < list.Count; i++)
{
this.txtWHMode.Text += list[i].NAME_1 + " ";
}

如何获取NAME_1不相等
...全文
162 7 打赏 收藏 转发到动态 举报
写回复
用AI写文章
7 条回复
切换为时间正序
请发表友善的回复…
发表回复
lizlqq5 2012-02-01
  • 打赏
  • 举报
回复
[Quote=引用 1 楼 viewstates 的回复:]

list.Distinct(...)

继承IEqualityComparer<VIWWHUSERDUTY>,实现接口方法即可
[/Quote]

这个怎么继承 你能告诉我吗 ,我没有玩过
lizlqq5 2012-02-01
  • 打赏
  • 举报
回复
[Quote=引用 5 楼 findcaiyzh 的回复:]

或者

List<string> tempList = new List<string>(list.Count);
for (int i = 0; i < list.Count; i++)
{
if (tempList.Contain(list[i].NAME_1)
continue;
tempList.Add(list[i].NAME_1);
……
[/Quote]

List<string> tempList = new List<string>(list.Count);
声明这个集合我没有看懂是什么意思
错误 6 参数“1”: 无法从“string”转换为“Model.VIWWHUSERDUTY” G:\开发工作\仓库\WarehouseSystem\WarehouseSystem\Model\MasterForm.aspx.cs 83 38 WarehouseSystem
宝_爸 2012-02-01
  • 打赏
  • 举报
回复
或者

List<string> tempList = new List<string>(list.Count);
for (int i = 0; i < list.Count; i++)
{
if (tempList.Contain(list[i].NAME_1)
continue;
tempList.Add(list[i].NAME_1);
this.txtWHMode.Text += list[i].NAME_1 + " ";
}

代码没有编译验证,只用于阐述思路。
宝_爸 2012-02-01
  • 打赏
  • 举报
回复
想1楼说的参考这里的代码

public class Product
{
public string Name { get; set; }
public int Code { get; set; }
}

// Custom comparer for the Product class
class ProductComparer : IEqualityComparer<Product>
{
// Products are equal if their names and product numbers are equal.
public bool Equals(Product x, Product y)
{

//Check whether the compared objects reference the same data.
if (Object.ReferenceEquals(x, y)) return true;

//Check whether any of the compared objects is null.
if (Object.ReferenceEquals(x, null) || Object.ReferenceEquals(y, null))
return false;

//Check whether the products' properties are equal.
return x.Code == y.Code && x.Name == y.Name;
}

// If Equals() returns true for a pair of objects
// then GetHashCode() must return the same value for these objects.

public int GetHashCode(Product product)
{
//Check whether the object is null
if (Object.ReferenceEquals(product, null)) return 0;

//Get hash code for the Name field if it is not null.
int hashProductName = product.Name == null ? 0 : product.Name.GetHashCode();

//Get hash code for the Code field.
int hashProductCode = product.Code.GetHashCode();

//Calculate the hash code for the product.
return hashProductName ^ hashProductCode;
}

}


Product[] products = { new Product { Name = "apple", Code = 9 },
new Product { Name = "orange", Code = 4 },
new Product { Name = "apple", Code = 9 },
new Product { Name = "lemon", Code = 12 } };

//Exclude duplicates.

IEnumerable<Product> noduplicates =
products.Distinct(new ProductComparer());

foreach (var product in noduplicates)
Console.WriteLine(product.Name + " " + product.Code);

/*
This code produces the following output:
apple 9
orange 4
lemon 12
*/



你可以修改Equals的实现,只判断Name是否相等
代码来自:
http://msdn.microsoft.com/en-us/library/bb338049.aspx
lizlqq5 2012-02-01
  • 打赏
  • 举报
回复
合并for循环的重复项 大侠们 怎么做啊
酷儿 2012-02-01
  • 打赏
  • 举报
回复
继承IEqualityComparer<VIWWHUSERDUTY>,实现接口方法即可
ViewStates 2012-02-01
  • 打赏
  • 举报
回复
list.Distinct(...)

继承IEqualityComparer<VIWWHUSERDUTY>,实现接口方法即可

62,046

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术交流专区
javascript云原生 企业社区
社区管理员
  • ASP.NET
  • .Net开发者社区
  • R小R
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

.NET 社区是一个围绕开源 .NET 的开放、热情、创新、包容的技术社区。社区致力于为广大 .NET 爱好者提供一个良好的知识共享、协同互助的 .NET 技术交流环境。我们尊重不同意见,支持健康理性的辩论和互动,反对歧视和攻击。

希望和大家一起共同营造一个活跃、友好的社区氛围。

试试用AI创作助手写篇文章吧