Loop Through LINQ Query Columns (Not rows)

前端 未结 3 1113
我寻月下人不归
我寻月下人不归 2021-01-18 22:01

Is it possible, and if so how, to loop though the results of a LINQ query?

Something like this:

var results= from a in dt.AsEnumerable()
                     


        
3条回答
  •  -上瘾入骨i
    2021-01-18 22:22

    If you have the option of changing your original LINQ statement to produce a data structure that allows you to do what you're looking for, I'd definitely suggest that.

    If not, you'll need to use reflection to look up the properties of your anonymous type by their name, and then get the values of those properties by reflection:

        PropertyInfo[] columns = results.First().GetType().GetProperties();
        ...
                string foobar = columns[i].GetValue(row, null);
    

提交回复
热议问题