Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/325.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/visual-studio-2012/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# DataTable:使用LINQ和条件字段(GroupBy)获取最大值_C#_Linq_Datatable - Fatal编程技术网

C# DataTable:使用LINQ和条件字段(GroupBy)获取最大值

C# DataTable:使用LINQ和条件字段(GroupBy)获取最大值,c#,linq,datatable,C#,Linq,Datatable,我有一个如下结构的数据表: 用户名|价格 “杰克” “杰克” “玛丽” “玛丽” 如何使用DataTable上的LINQ返回JACK的最高价格(例如:.02) 设置表格的代码(可以忽略) 这就是我被卡住的地方。想要以杰克的最高价格(例如:“.02”)返回一行 我不知道如何让VisualStudio将“Price”字段识别为强类型。我猜这和这个问题有关 当然,可以使用两个查询(一个是按用户名过滤,另一个是选择Max,但它没有那么优雅) 与有关。几乎什么都试过了/到处看,但运气不佳 谢谢。由于Dat

我有一个如下结构的数据表:

用户名|价格

“杰克”

“杰克”

“玛丽”

“玛丽”

如何使用DataTable上的LINQ返回JACK的最高价格(例如:.02)

设置表格的代码(可以忽略)

这就是我被卡住的地方。想要以杰克的最高价格(例如:“.02”)返回一行

我不知道如何让VisualStudio将“Price”字段识别为强类型。我猜这和这个问题有关

当然,可以使用两个查询(一个是按用户名过滤,另一个是选择Max,但它没有那么优雅)

与有关。几乎什么都试过了/到处看,但运气不佳

谢谢。

由于
DataRow
上没有
price
成员,您无法通过这种方式访问“price”。访问它就像使用
username
一样(按列名):

当然,您可以简单地执行以下操作:

string max = tbl.AsEnumerable()
        .Where(row => row["username"].ToString() == "jack")
        .Max(row => row["price"])
        .ToString();
您是否尝试过:

var query = tbl.AsEnumerable().Where(tr => (string) tr["username"] == "jack")
               .Max(tr => (double) tr["price"]);

但是,由于您只允许一个用户,因此不需要分组。只需按照@WorldIsRound的建议选择最大值。

g.Max(x=>x[“价格”);或g.Max(x=>x.Field(“价格”);太好了!不知道其他示例如何使用强类型属性,但那是另一天的事了…我非常喜欢这个。希望我能选择两个答案。你可以通过单击向上箭头来向上投票任何你喜欢的问题或答案。这些年来,我都不知道你可以像这样来投栏,而不是z.field[“RENT”]vs(double)[“RENT”]所以…+1
var result =
    from row in tbl.AsEnumerable() 
    where (string) row["username"] == "jack"
    group row by new {usernameKey = row["username"]} into g  
    select new
    { 
        jackHighestPrice = g.Max(x => x["price"])
    };
string max = tbl.AsEnumerable()
        .Where(row => row["username"].ToString() == "jack")
        .Max(row => row["price"])
        .ToString();
var query = tbl.AsEnumerable().Where(tr => (string) tr["username"] == "jack")
               .Max(tr => (double) tr["price"]);
var result =
    from row in tbl.AsEnumerable()
    where (string)row["username"] == "jack"
    let x = new { usernameKey = (string)row["username"], (double)price = row["price"] }
    group x by x.usernameKey into g
    select g.Max(x => x.price);