Remove property / column from general list

For some reason, I cannot change the request, 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 that I receive is displayed in this list. Now I want to remove these properties from the list with values null. I may seem crazy, but you read it right. I was thinking of creating another list with only the selected properties, but any of the above properties may be null. Therefore, I must develop a filtering mechanism for my list based on this.

For clarity, consider the following example.

List<myClass> lstClass = some data source.

After receiving the data, the general list (lstClass) is as follows. Consider the result set in the table:

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

- sdate. , , .

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

? Linq?

PS: . , , , .

+5
2

, myClass, :

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"
}
+15

, Data, . ? , DataGrid AutoGenerateColumns = True, : 1) / 2) column/property, , Collapsed. , : , .
, , , null . , , , , , Reflection // .

0

All Articles