データベース フィールドに数値のみが含まれていることが確実な場合は、nvarchar フィールドに常にmaxを使用できます。以下の例を参照してください。
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; }
}
}