循环遍历 LINQ 查询列(而不是行)

发布于 2024-12-12 03:17:59 字数 883 浏览 0 评论 0原文

是否可以循环 LINQ 查询的结果?如果可以,如何循环?

像这样的:

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++;
    }
}

本质上,我想复制以下功能:

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

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

一场信仰旅途 2024-12-19 03:18:00

如果您可以选择更改原始 LINQ 语句以生成允许您执行所需操作的数据结构,我绝对会建议这样做。

如果没有,您需要使用反射按名称查找匿名类型的属性,然后通过反射获取这些属性的值:

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

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);
千鲤 2024-12-19 03:18:00
Action<string> processColumName = (cname) => 
                           { 
                              // do anything you want with a column name
                              string foo = (string) Row[cname]; 
                           };

results.First()
        .GetType()
        .GetProperties()
        .Select(p => p.Name)
        .ToList().ForEach(processColumName);
Action<string> processColumName = (cname) => 
                           { 
                              // do anything you want with a column name
                              string foo = (string) Row[cname]; 
                           };

results.First()
        .GetType()
        .GetProperties()
        .Select(p => p.Name)
        .ToList().ForEach(processColumName);
梦醒时光 2024-12-19 03:18:00

嗯,我的答案实际上是基于 stovroz 答案 来自另一个问题,稍作修改:

所以这里是代码:

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

Well my answer actually is based on stovroz answer from another question, slightly modified:

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));
   }  
} 
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文