如果您确定您的数据库字段将只包含数值,那么您始终可以将max用于 nvarchar 字段。见下面的例子:
var serials = new List<SerialObject>();
// Add AlphaNumeric value to the list
serials.Add(new SerialObject()
{
Id = "1",
Serial = "aa10a1"
});
// Add AlphaNumeric value to the list
serials.Add(new SerialObject()
{
Id = "2",
Serial = "cc11da"
});
// Add Numeric values to the list
for (int i = 0; i < 5; i++)
{
serials.Add(new SerialObject()
{
Id = i.ToString(),
Serial = (1000 + i).ToString()
});
}
// Fetch max numeric from list using regular expression
var regex = new Regex("^[0-9]*$");
var numbers = serials.Where(a => regex.IsMatch(a.Serial)).Select(a => a.Serial).ToList();
var max = numbers.Max(a => a);
串行对象类
public class SerialObject
{
public string Id { get; set; }
public string Serial { get; set; }
}
}