Search code examples
c#listclassvisual-studio-lightswitch

Remove columns from list object out of a class c#


I need the possiblity to create Code in C# like this

public class SummaryA 
{
    public string name { get; set; }
    public string surename { get: set; }
    public int age { get; set;}
}

now I create an list object from the class SummaryA

List<SummaryA> list1= new List<SummaryA>(); 

yet I need the possibility to remove the column age from the list Summary, anyone have ideas? I need this for some more columns, so I wish the list was dynamically or some things else.

sry for my bad english.


Solution

  • To completely remove the column you really need another class to store the data in, for example:

    public class AgelessSummaryA 
    {
        public string name { get; set; }
        public string surename { get: set; }
    }
    

    And now you can project the first list into a new list of this class with Linq and Select:

    List<AgelessSummaryA> agelessSummaries = ageSummaries
        .Select(s => new AgelessSummaryA
        {
            name = s.name,
            surename = s.surename
        })
        .ToList();