简体   繁体   English

从列表中删除对象

[英]Remove object from List

My List is 我的清单是

public List<BaseHomePage.QueueListItem> QueueDataSource
   {
    get { return this._queueDataSource; }
    set { this._queueDataSource = value; }
   }

and I want to remove the object from the list,my code is 我想从列表中删除对象,我的代码是

for (int i = 0; i < _queueDataSource.Count; i++)
{
    object queue = _queueDataSource[i];

    if (objQuery.BranchOutQueue)
    {
        this._queueDataSource.Remove(queue);   //Here I want to getting error
    }
}

Error : The best overload method match for system.collections.Generic.List.Remove(BaseHome.QueueList) has some invalid arguments 错误:system.collections.Generic.List.Remove(BaseHome.QueueList)的最佳重载方法匹配具有一些无效的参数

Instead of using object use var . 而不是使用object而是使用var

var queue = _queueDataSource[i];

Or to be specific you can use: 或者,您可以使用:

BaseHomePage.QueueListItem queue = _queueDataSource[i];

The reason you are getting the error is that Remove expects parameter of the same type as of List, since you are getting the object retruned in type object its not compatible. 出现错误的原因是Remove期望参数的类型与List的类型相同,因为您将对象转换为类型不兼容的object

Just use the RemoveAt instead base from the object 只需使用RemoveAt代替object

     for (int i = 0; i < _queueDataSource.Count; i++)
        {
            if (IsSuccess)
             _queueDataSource.RemoveAt(i)
        }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM