简体   繁体   中英

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

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM