C# 从DataTable-Varchar()列中查找Max()值?

C# 从DataTable-Varchar()列中查找Max()值?,c#,C#,我有一个数据表MyDtb1。我有列“sl_no”varchar(10); 使用此sl_no column,我的值如下所示 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 MyDtb1.sl_无列值 搜索-第0行 第一至第一排 第2-2排 第三至第三排 第4-4排 空-第5行 从上面我想选择sl_no的最大值,结果必

我有一个数据表MyDtb1。我有列“sl_no”varchar(10); 使用此sl_no column,我的值如下所示

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 MyDtb1.sl_无列值 搜索-第0行 第一至第一排 第2-2排 第三至第三排 第4-4排 空-第5行 从上面我想选择sl_no的最大值,结果必须是“4”

谢谢你的建议。

像这样的吗

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();
List testList=新列表{
“搜索”,
"1",
"2",
"3",
"4",
无效的
};
int-num;
int maxNumeric=testList.Where(x=>int32.TryParse(x,out num)).Select(x=>Convert.ToInt32(x)).Max();
intx;
var maxVal=MyDtb1.AsEnumerable()
.Max(r=>int.TryParse(r.Field(“sl_no”),out x)?(int?)x:null;

是否有某种原因导致您解析为
double
,然后转换为
int
?Int32有自己的
TryParse
方法…@djacobson,没有好的理由=)输入太快,无法思考-更正,谢谢
    int x;
    var maxVal = MyDtb1.AsEnumerable()
      .Max (r => int.TryParse(r.Field<string>("sl_no"), out x) ? (int?)x : null);