VB.NET LINQ To DataTable 使用 Where 子句选择

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/36631483/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-17 19:48:27  来源:igfitidea点击:

VB.NET LINQ To DataTable Select with Where Clause

vb.netlinqdatatable

提问by Dillon Willis

Here is my data table that I have stored in a data table variable named dt:

这是我存储在名为 dt 的数据表变量中的数据表:

Dim dt As New DataTable
+---------+--------+---------------+
| Carrier | AVGTAT |     Name      |
+---------+--------+---------------+
| ABCD    |   2078 | Term Check    |
| ABCD    |      0 | AdHoc         |
| ABCD    |  26406 | Cash on Term  |
| REWS    |   7358 | Failed Bill   |
| ZELT    |  11585 | BL150         |

I need to get the value of the AVGTAT column using LINQ to DataTable based Where Carrier = "x" and Name = "X"

我需要使用基于 LINQ to DataTable 的 Where Carrier = "x" and Name = "X" 来获取 AVGTAT 列的值

How can I accomplish that?

我怎样才能做到这一点?

Thank you!

谢谢!

回答by Shyju

Here is the C# version.

这是 C# 版本。

var avg = (from t1 in dt.AsEnumerable()
                select new
                {
                    Carrier = t1.Field<string>("Carrier"),
                    Name = t1.Field<string>("Name"),
                    Avg = t1.Field<int>("AVGTAT")

                }).Where(s => s.Carrier == "X" && s.Name == "X")
                                                      .Select(v=>v.Avg).FirstOrDefault();

And the VB.NET version

和 VB.NET 版本

Dim avg = dt.AsEnumerable().[Select](Function(x) New With {
       Key .Carrier = x.Field(Of String)("Carrier"),
       Key .Name = x.Field(Of String)("Name"),
       Key .Avg = x.Field(Of Int32)("Level")
   }).Where(Function(s) s.Carrier = "X" AndAlso s.Name = "X")
                                            .[Select](Function(h) h.Avg).FirstOrDefault()