Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to remove an element from a list of user define class?

Tags:

arrays

c#

list

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?

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

like image 813
Clayton Leung Avatar asked Jun 03 '12 12:06

Clayton Leung


1 Answers

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

ask.RemoveAll(a => a.price == 300 && a.volume == 400);
like image 190
Prashanth Thurairatnam Avatar answered Sep 23 '22 02:09

Prashanth Thurairatnam