views:

102

answers:

3

Hi,

I have a class MyObj and a collection IEnumerable. Some of the columns are wholly empty (i.e. == NULL) across all rows and therefore I want to create an IEnumerable<> of the members of MyObj which hold a non-null value.

If I could predict the members of MyObj which would be of interest I'd do something like:

var part = 
    from entry in iList  
    select new {entry.a, entry.c, entry.s};

...but I don't know which members of MyObj I'm interested in at design time - I only know that at runtime.

How can I construct my list??

Thanks,

Tamim Sadikali.

A: 

Your question does not make sense.

You're trying to create a type whose members are only known at runtime.
What would you do with the results?
You would not be able to access any properties of the result objects because they might not exist.

If you want to display the data in a grid, and you don't want to display columns which are entirely null, then you should bind the original collection to the grid, then hide some of the columns in the grid.

SLaks
A: 

Wait for release of VS2010, C# 4.0 with it's 'dynamic' type should solve your problem. (Or maybe help you shoot yourself in the foot).

Yossarian
The `dynamic` keyword won't help here; the question does not make any sense.
SLaks
But MethodBags should?
Yossarian
A: 

If you are doing this for UI, better hide columns that contain all nulls. For DataGridView in WinForms it may look like this:

foreach (DataGridViewColumn column in dataGridView.Columns)
    if (dataGridView1.Rows.Cast<DataGridViewRow>().All(r => r.Cells[column.Name].Value == null))
        column.Visible = false;
Konstantin Spirin