如何从用户定义类列表中删除元素?

时间:2022-06-30 19:00:10

I am fairly new to c#, please be gentle to me, I have been searching through the net for a few hours without success, I want to remove an element from my user defined class. How do I do it?

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

below is the snippet of the code.

下面是代码的片段。

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

 }

I think I need to implement IEnumerable of some sort, but how does the syntax look like? can someone please give me a working snippet? thanks a lot

我想我需要实现某种IEnumerable,但语法是怎样的?有人可以给我一个工作片段吗?非常感谢

4 个解决方案

#1


9  

This will remove all Level2 objects with Price = 300 and Volume = 400 from the list

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

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

#2


2  

Here's how I'd do it: override Equals and GetHashCode on Level2, so that you can use List<T>.Remove without needing the original reference to the object.

我是这样做的:在Level2上覆盖Equals和GetHashCode,这样就可以使用List .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));
}

Here's another way, which works off of the identity of the object instead of the values inside of it:

这是另一种方式,它取决于对象的身份而不是其中的值:

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);
}

Depending on your situation, one of these, or one of the other answers, may be the best approach.

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

#3


1  

Remember that you are adding instantiated objects to the typed List bid, so you'll need to search your list for objects to remove that match a certain condition. Note that you could have multiple (different) instances of Level2 in your list with price==300 and volume==400.

请记住,您要将实例化的对象添加到键入的列表出价中,因此您需要在列表中搜索要删除匹配特定条件的对象。请注意,您的列表中可能有多个(不同的)Level2实例,其中price == 300且volume == 400。

A small code sample should clarify things.

一个小代码示例应该澄清事情。

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());
    }
}

#4


0  

You need to first retrieve the element you want to remove:

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

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

#1


9  

This will remove all Level2 objects with Price = 300 and Volume = 400 from the list

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

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

#2


2  

Here's how I'd do it: override Equals and GetHashCode on Level2, so that you can use List<T>.Remove without needing the original reference to the object.

我是这样做的:在Level2上覆盖Equals和GetHashCode,这样就可以使用List .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));
}

Here's another way, which works off of the identity of the object instead of the values inside of it:

这是另一种方式,它取决于对象的身份而不是其中的值:

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);
}

Depending on your situation, one of these, or one of the other answers, may be the best approach.

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

#3


1  

Remember that you are adding instantiated objects to the typed List bid, so you'll need to search your list for objects to remove that match a certain condition. Note that you could have multiple (different) instances of Level2 in your list with price==300 and volume==400.

请记住,您要将实例化的对象添加到键入的列表出价中,因此您需要在列表中搜索要删除匹配特定条件的对象。请注意,您的列表中可能有多个(不同的)Level2实例,其中price == 300且volume == 400。

A small code sample should clarify things.

一个小代码示例应该澄清事情。

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());
    }
}

#4


0  

You need to first retrieve the element you want to remove:

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

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