【问题标题】:how to remove an element from a list of user define class?如何从用户定义类列表中删除元素?
【发布时间】:2012-06-03 12:00:09
【问题描述】:

我是c#的新手,请对我温柔,我已经在网上搜索了几个小时没有成功,我想从我的用户定义类中删除一个元素。我该怎么做?

下面是代码的sn-p。

public class Level2
{
    public double price { get; set; }
    public long volume { get; set; }

    public Level2(double price, long volume)
    {
        this.price = price;
        this.volume = volume;
    }
}

static void Main()
{

    List<Level2> bid = new List<Level2>();

    ask.Add(new Level2(200, 500));
    ask.Add(new Level2(300, 400));
    ask.Add(new Level2(300, 600));


    // how to remove this element ???
    ask.Remove(300, 400);       //doesn't work

 }

我认为我需要实现某种类型的 IEnumerable,但语法看起来如何?有人可以给我一个工作的sn-p吗?非常感谢

【问题讨论】:

  • askbid是什么关系?它们应该是同一个变量吗?

标签: c# arrays list


【解决方案1】:

这将从列表中删除所有具有 Price = 300 and Volume = 400Level2 对象

ask.RemoveAll(a => a.price == 300 && a.volume == 400);

【讨论】:

  • 很高兴我能帮上忙;如果它回答了您的问题,您可以将其标记为答案。
【解决方案2】:

我的做法如下:覆盖Level2 上的EqualsGetHashCode,这样您就可以使用List&lt;T&gt;.Remove 而无需对对象的原始引用。

public class Level2
{
    public double price { get; set; }
    public long volume { get; set; }

    public Level2(double price, long volume)
    {
        this.price = price;
        this.volume = volume;
    }

    public override bool Equals(object obj)
    {
        var other = obj as Level2;
        if (other == null)
            return false;
        return other.price == this.price && other.volume == this.volume;
    }

    public override int GetHashCode()
    {
        return price.GetHashCode() ^ volume.GetHashCode();
    }
}

static void Main(string[] args)
{
    List<Level2> ask = new List<Level2>();

    ask.Add(new Level2(200, 500));
    ask.Add(new Level2(300, 400));
    ask.Add(new Level2(300, 600));

    ask.Remove(new Level2(300, 400));
}

这是另一种方式,它根据对象的身份而不是对象内部的值来工作:

static void Main(string[] args)
{
    List<Level2> ask = new List<Level2>();

    ask.Add(new Level2(200, 500));
    var level = new Level2(300, 400);
    ask.Add(level);
    ask.Add(new Level2(300, 600));

    ask.Remove(level);
}

根据您的情况,其中一个或其他答案之一可能是最好的方法。

【讨论】:

    【解决方案3】:

    请记住,您正在将实例化的objects 添加到类型化列表bid,因此您需要在列表中搜索对象以删除符合特定条件的对象。请注意,您的列表中可能有多个(不同的)Level2 实例,price==300volume==400

    一个小的代码示例应该可以说明问题。

    public class Level2
    {
        public double price { get; set; }
        public long volume { get; set; }
        public Level2(double price, long volume)
        {
            this.price = price;
            this.volume = volume;
        }
        public override string ToString()
        {
            return String.Format("Level 2 object with price: {0} and volume: {1}", 
                this.price, this.volume);
        }
    }
    
    public static void Main()
    {
        List<Level2> bid = new List<Level2>();
        bid.Add(new Level2(200, 500));
        bid.Add(new Level2(300, 400));
        bid.Add(new Level2(300, 600));
        bid.Add(new Level2(300, 400)); // second item with these quantities
    
        List<Level2> toRemove = 
            bid.Where(x => x.price == 300 && x.volume == 400).ToList<Level2>();
    
        foreach (Level2 item in toRemove)
        {
            bid.Remove(item);
            Console.WriteLine("removing " + item.ToString());
        }
    }
    

    【讨论】:

    • 也许我错了,但你确定吗?我的意思是从您正在迭代的列表中删除一个项目?
    • @Steve:你是对的,我已经更新了我的答案,因为那是自找麻烦;-)
    【解决方案4】:

    您需要先检索要删除的元素:

    Level2 element = ask.FirstOrDefault(l => l.price == 300 && l.volume == 400);
    if (element != null)
        ask.Remove(element);
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2021-03-10
      • 1970-01-01
      • 2010-10-13
      • 2020-07-23
      • 2021-08-18
      • 1970-01-01
      相关资源
      最近更新 更多