English 中文(简体)
Linq: Sum摘要
原标题:Linq: Sum of int
  • 时间:2009-10-12 14:33:31
  •  标签:

I am getting "The null value cannot be assigned to a member with type System.Int32 which is a non-nullable value type" When executing Sum() of my empty statement. ResultView works fine, but 或者

var r = from v in DataContext.Visits
        join bs in DataContext.BaseContents on v.BaseContentID equals bs.Id
        where (bs.CreatedBy == userId) && (v.DateVisited.Year == workDate.Year) &&
        (v.DateVisited.Month == workDate.Month) && (v.DateVisited.Day == workDate.Day) &&
        (v.IsPreviewed == false) && (bs.ProfileProjectId != null)
        select v;

int? number = r.Sum( v => v.Counter);

或者

var r = from v in DataContext.Visits
        join bs in DataContext.BaseContents on v.BaseContentID equals bs.Id
        where (bs.CreatedBy == userId) && (v.DateVisited.Year == workDate.Year) &&
        (v.DateVisited.Month == workDate.Month) && (v.DateVisited.Day == workDate.Day) &&
        (v.IsPreviewed == false) && (bs.ProfileProjectId != null)
        select v.Counter;

int? number = r.Sum(v);

完全没有例外。

问题回答

(updated):

int number = r.Sum(v => (int?)v.Counter) ?? 0;

您能否列入一些样本数据? 至少,你们可能贪 gr。 从我能看到的角度来看,它应当做的是罚款。 还确保这一点与BaseContentID一样。 Id 和 v.DateVisited 不可否认。 (特别是ID栏) 凡是提到无效的分类,都可能造成这种例外。 不仅仅是Select。 条款

var r = from v in DataContext.Visits
        join bs in DataContext.BaseContents on v.BaseContentID equals bs.Id
        where (bs.CreatedBy == userId) 
            && (v.DateVisited.Date == workDate.Date)
            && (!v.IsPreviewed) 
            && (bs.ProfileProjectId.HasValue)
        select v;

int? number = r.Sum(v => v.Counter);

Seems such as putting && (v.Counter != 无),在管理这笔金额之前,将可对所有价值无效的人进行过滤。





相关问题
热门标签