开发者

Find Max() Value from DataTable - Varchar() Column?

开发者 https://www.devze.com 2023-03-16 23:55 出处:网络
I have a DataTable MyDtb1. And i have column \"sl_no\" varchar(10); With this sl_no coloumn I have the value like the below manner.

I have a DataTable MyDtb1. And i have column "sl_no" varchar(10); With this sl_no coloumn I have the value like the below manner.

MyDtb1.sl_no Co开发者_JS百科lumn Values

Search - Zero'th Row
1 - 1 st Row
2 -2 nd Row
3 - 3 rd Row
4 - 4 th Row
Null - 5 th row

From the above I want to select the MAX(value) of sl_no and the result has to be "4"

Thanks for the Ideas.


    int x;
    var maxVal = MyDtb1.AsEnumerable()
      .Max (r => int.TryParse(r.Field<string>("sl_no"), out x) ? (int?)x : null);


Something like this?

List<string> testList = new List<string> {
    "Search",
    "1",
    "2",
    "3",
    "4",
    null                
    };
int num;
int maxNumeric = testList.Where(x => int32.TryParse(x, out num)).Select(x => Convert.ToInt32(x)).Max();
0

精彩评论

暂无评论...
验证码 换一张
取 消