我有一个类似下面的类,我试图获取特定区域的最大值:
public partial class Ads
{
public int Id { get; set; }
public int RegionId { get; set; }
public int Charge { get; set; }
public Nullable<int> Group { get; set; }
}
记录:
Id RegionId Charge Group
1 100 100 1
现在我正在尝试获取regionid = 100的最大值,但问题是我总是1以下查询:
int group = context.Ads.Where(a => a.RegionId == 100).Max(t => (int?)t.Group)) ?? 0 + 1;
为什么上面只给我 1而不是2 ?
答案 0 :(得分:5)
您应该将??
括在括号中:
int group = (context.Ads.Where(a => a.RegionId == 100).Max(t => (int?)t.Group) ?? 0) + 1;
原因是??
的优先级非常低,如果您不在代码末尾使用parantheses 0 + 1
,则会先计算它,它将等于:
int group = context.Ads.Where(a => a.RegionId == 100).Max(t => (int?)t.Group) ?? 1;
// as 0 + 1 equals to 1
答案 1 :(得分:2)
问题是它总是0,你需要把内部()
Date.prototype.toNormalizedDate = function(){
var normalizedDate = new Date(Date.UTC(this.getFullYear(), this.getMonth(), this.getDate(), this.getHours(), this.getMinutes(), this.getSeconds()));
return normalizedDate.toISOString();
}