环通LINQ查询塔(未行) [英] Loop Through LINQ Query Columns (Not rows)

查看:97
本文介绍了环通LINQ查询塔(未行)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是否有可能,如果又如何,循环虽然LINQ查询的结果

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

事情是这样的:

var results= from a in dt.AsEnumerable()
                    where a.Field<int>("id") == i 
                    select new 
                    {
                       id= a.Field<int>("id"),
                       a= a.Field<double>("a"),
                       b = a.Field<double>("b")
                    };

IEnumerable<string> colNames = results.First().GetType().GetProperties()
                                                        .Select(p => p.Name);

string[] columns = colNames.ToArray();

int i = 0;
foreach (var row in results)
{
    for (int i = 0; i < columns.Count(); i++)
    {
        string foobar = (string)row[columns[i]];
        i++;
    }
}

从本质上讲,我希望复制下面的排序功能

Essentially, i want to replicate the following sort of functionality:

DataRow dr = new DataRow();
string foobar = dr[columns[i];



感谢所有提前。

Thanks all in advance.

推荐答案

如果你有改变你原有的LINQ语句产生的数据结构,让你做你要找的内容的选择,我肯定建议。

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);

这篇关于环通LINQ查询塔(未行)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆