Remove a property/column from a generic list

21,334

Solution 1

Assuming you have a generic list of myClass instances, you can create an anonymous type with only the needed properties:

List<myClass> list = ...;
var reducedList = list.Select(e => new {e.id, e.name, e.status}).ToList();
// note: call to ToList() is optional

foreach (var item in reducedList)
{
    Console.WriteLine(item.id + " " + item.name + " " + item.status);
    //note: item does not have a property "sDate"
}

Solution 2

I'm not sure you should solve your issue in the Data, but rather it's a presentation problem. In which control do you want to display it ? Let's say you display it in DataGrid with AutoGenerateColumns=True, then you can 1) loop on columns/properties 2) for each column/property see if all property values for all rows are null and if so set column's visibility to Collapsed. If you generate your columns by yourself it's even simpler : only add columns when content is not null for all rows.
If your DB content is dynamic, you might want to bind each row's visibility to a property that would state wether all rows are null or not for that property. Depending on how generic you want your code to be, the code might be very different, and in case you want to have generic solution, using Reflection to retrieve/get/set properties might be of some use.

Share:
21,334
ankur
Author by

ankur

Updated on February 03, 2020

Comments

  • ankur
    ankur about 4 years

    Due to some reason I cannot change the query so I have to do this in C#.

    I have a class:

    public class myClass
    {
        int id { get; set; }
        string name { get; set; }
        DateTime sDate { get; set; }
        bool status { get; set; } 
    }
    

    The data I am getting is fetched in this list. Now what I want is to remove those properties from a list that has null values. I may sound insane but you read it right. I thought of creating another list with only the selected properties, but any of the above properties can be null. So I have to devise a mechanism to filter my list based on this.

    For more clarity consider the following example.

    List<myClass> lstClass = some data source.
    

    After getting the data the generic list(lstClass) looks like this.Consider the result set in a table:

    Id Name Sdate status
    1  a    null  null
    2  b    null  null
    3  c    null  false
    

    Can i some how make my list look like this after removing the property sdate. So the new list that I want to create should have only three properties.

    Id Name status
    1  a    null
    2  b    null
    3  c    false
    

    Any ideas? Can I do this using Linq?

    PS: This has nothing to do with presentation. I don’t have a grid where I am not able to hide columns that Is not what I am looking for.

  • ankur
    ankur about 12 years
    @M4N but i dont know which column will be blank so i cant hard cord it. can you provide an example where you first determine the chunk of column name that has to be passed in anonymous class.
  • GameAlchemist
    GameAlchemist about 12 years
    your ability to listen to other people's advice to get to a solution is quite impressive.