Loop Through LINQ Query Columns (Not rows)

前端 未结 3 1115
我寻月下人不归
我寻月下人不归 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条回答
  •  孤城傲影
    2021-01-18 22:09

    Well my answer actually is based on someone's else answer (I will be leaving a link below) with sligh changes: stovroz

    So here is the code:

    foreach(var item in db.Products) {
       System.Reflection.PropertyInfo[] fields = Product.GetType().GetProperties(); 
       foreach(var f in fields) {
          Console.WriteLine(f.Name + ": " + f.GetValue(item));
       }  
    } 
    

    Actually I was looking for such a solution to reach dynamic LINQ usage and this guy saved my day. Huge thanks to him!

提交回复
热议问题