使用Contains()方法检查列表中的项目 [英] Check an item on a list with Contains() method

查看:99
本文介绍了使用Contains()方法检查列表中的项目的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

你好,我有一个名为AddToGrid的类和一个通用列表.
AddToGrid类具有ID,PART,QTY和WEIGHT等属性.

从gridview向列表中添加新项目时,我想检查列表中是否包含具有相同ID的相同项目.如果没有匹配的ID,我会将其添加到列表中.
我写的代码;

Hello I have a class named AddToGrid and a generic list.
AddToGrid class has properties such as ID, PART, QTY and WEIGHT.

While adding new items to the list from a gridview I want to check if the list has a same item with the same ID or not. if it doesnt have a matching ID I will add it to the list.
The code that I wrote;

for (int i = 0; i < gvParts.Rows.Count; i++)//loop the GridView Rows
        {
            AddToGrid mainList = new AddToGrid();
            mainList.ID = Convert.ToInt32(gvParts.Rows[i].Cells[1].Text);
            mainList.PART = gvParts.Rows[i].Cells[2].Text;
            mainList.QTY = Convert.ToInt32(gvParts.Rows[i].Cells[5].Text);
            string weightlbs = gvParts.Rows[i].Cells[3].Text;
            string[] weight = weightlbs.Split('' '');
            mainList.WEIGHT = Convert.ToDecimal(weight[0]);
            if (rmaPartLstDisp.Count == 0)
            {
                rmaPartLstDisp.Add(mainList);
            }
            else
            {                
                for (int j = 0; j < rmaPartLstDisp.Count; j++)
                {
                    if (!rmaPartLstDisp.Contains(mainList))
                    {
                        rmaPartLstDisp.Add(mainList);                        
                    }
                }
            }
      )      



在if语句的for循环内,我需要有类似的内容;



Inside the for loop on the if statement I need to have something like;

for (int j = 0; j < rmaPartLstDisp.Count; j++)
               {
                   if (!rmaPartLstDisp.Contains(mainList.ID))
                   {
                       rmaPartLstDisp.Add(mainList);
                   }
               }


但是由于无效的参数而导致错误.
有人可以帮助我找到一种适当的方法进行检查吗?谢谢.


But it gives an error because of the invalid arguments.
Can someone help me find a proper way to check this? Thanks.

推荐答案

请考虑以下这部分代码:
Consider this part of your code:
if (rmaPartLstDisp.Count == 0)
{
    rmaPartLstDisp.Add(mainList);
}
else
{
    for (int j = 0; j < rmaPartLstDisp.Count; j++)
    {
        if (!rmaPartLstDisp.Contains(mainList))
        {
            rmaPartLstDisp.Add(mainList);
        }
    }
}



首先,您检查列表计数是否为零.如果是,则它不能包含您的mainList实例,因此您将其添加.如果计数大于零,则要添加该计数(如果不存在).

因此,无论rmaPartLstDisp中的项目数如何,您都想添加mainList实例(如果该实例不存在的话).那为什么还要麻烦做所有的检查呢?

只需将所有内容替换为:



First you check if the list count is zero. If it is then it cannot contain your mainList instance, so you add it. If the count is greater than zero you want to add it if it is not there.

So, regardless of the number of items in rmaPartLstDisp you want to add the mainList instance if it isn''t there. Why bother doing all that checking then?

Simply replace all of that with:

if (!rmaPartLstDisp.Contains(mainList))
{
    rmaPartLstDisp.Add(mainList);
}



如果那是以前抛出的错误,它可能仍然会出错.因此,修改您的问题以添加新代码并复制 exact 错误消息.
[在OP评论后添加了修改]



If that was throwing an error before, it probably still will. So modify your question to add the new code and copy the exact error message.

if (rmaPartLstDisp.Count == 0)
{
    rmaPartLstDisp.Add(mainList);
}
else
{
    bool itemFound = false;
    foreach (AddToGrid atg in rmaPartLstDisp)
    {
        if (atg.ID == mainList.ID)
        {
            itemFound = true;
            break;
        }
    }
    if (!itemFound)
    {
      rmaPartLstDisp.Add(mainList);
    }
}


应该做你想要的.
[/Edit]


should do what you want.
[/Edit]


您没有显示类AddToGrid,仅此而已.您的问题很可能是通过类型区分大小写解决的,但是这种方法是错误的.无论如何,不​​好的类,不好的类名(应该是名词,而不是动词,Microsoft有很好的命名约定).您的问题是缺少泛型.

您可能需要一个泛型类,其中item类是一个泛型参数.列表数据可以表示为System.Collections.Generic接口和类.

另外,如果许多项目的缓慢操作,请使用Contains作为列表方法.为了快速操作,可以使用SystemCollections.Generic.Dictionary.

-SA
You don''t show the class AddToGrid, that''s it. Most likely, your problem is resolved by type case, but the approach is wrong. Anyway, bad class, bad class name (should be noun, not a verb, there are good Microsoft naming conventions). Your problem is lack of generics.

You may need a generic class with item class being a generic parameter. List data can be presented as System.Collections.Generic interfaces and classes.

Also, Contains as a list method if slow operation of there are many items. For fast operation SystemCollections.Generic.Dictionary can be used.

—SA


1.您可以使用字典
1. you could use a dictionary
Dictionary<int, AddToGrid> mycontainer = new Dictionary<int, AddToGrid>();

if(!mycontainer.ContainsKey(myid))
//add it



否则,contains是基于ref的搜索,您可以创建icomparere或执行类似的操作



otherwise the contains is a ref based search you can either create a icomparere or do something like

List<AddToGrid> mylist = new List<AddToGrid>();
if(mylist.FirstOrDefault<AddToGrid>( (item)=>item.Id==mynewId ) !=null)
    //add me


这篇关于使用Contains()方法检查列表中的项目的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆