我的数据库中有一个名为“ GRNHDReturn”的表。当表为空时,我想从表中获取最大ReturnId。我怎样才能做到这一点?
GRNHD返回数据库
public string getMax()
{
GRNHDReturn grh = new GRNHDReturn();
int max = 0;
if ((context.GRNHDReturns.Max(p => p.ReturnId) == 0))
{
max = 1;
calculatemax = "RN" + max.ToString();
}
else
{
max = context.GRNHDReturns.Max(p => p.ReturnId);
int nextmax = max + 1;
calculatemax = "RN" + nextmax.ToString();
}
return calculatemax;
}
最佳答案
public string getMax()
{
GRNHDReturn grh = new GRNHDReturn();
int? max = (from o in context.GRNHDReturns
select (int?)o.ReturnId).Max();
if (max == 0 || max == null)
{
max = 1;
calculatemax = "RN" + max.ToString();
}
else
{
int? nextmax = max + 1;
calculatemax = "RN" + nextmax.ToString();
}
return calculatemax;
}
你可以试试看