Linq DBNull的链接错误

Linq DBNull的链接错误,linq,Linq,我在c中有这个数据表结果 Date Employee Job1 Job2 Job3 1/1/2012 a 1 1 1 1/1/2012 b 2 1/1/2012 c 2 1 4 1/1/2012 d 4 2 1 1/2/2012 a 3 2 5 1/2/2012 b 2 2 2 1/2

我在c中有这个数据表结果

Date    Employee Job1   Job2   Job3
1/1/2012    a    1      1      1 
1/1/2012    b           2      
1/1/2012    c    2      1      4
1/1/2012    d    4      2      1
1/2/2012    a    3      2      5
1/2/2012    b    2      2      2
1/2/2012    c    3      3      3
1/2/2012    d    1      1      1
1/3/2012    a    5      5      5
1/3/2012    b    2      2      6
1/3/2012    c           1      1
1/3/2012    d    2      3      4
2/1/2012    a    2      2      2
2/1/2012    b    5      5      2
2/1/2012    c    2      2      2
2/2/2012    a           3      
2/2/2012    b    2      3      3
3/1/2012    a    4      4      2
要获得此结果,请执行以下操作:

工作1:

Linq代码是:

            var monthEmpGroups = tblEmpJobs.AsEnumerable()
                .Select(r => new
                {
                    Row = r,
                    Employee = r.Field<String>("Employee"),
                    Year = r.Field<DateTime>("Date").Year,
                    Month = r.Field<DateTime>("Date").Month
                })
                .GroupBy(x => x.Employee);

            DataTable tblMonthResultJob1 = new DataTable();
            tblMonthResultJob1.Columns.Add("Employee", typeof(string));
            var dtf = System.Globalization.CultureInfo.CurrentCulture.DateTimeFormat;

            foreach (var empGroup in monthEmpGroups)
            {
                string employee = empGroup.Key;
                var newRow = tblMonthResultJob1.Rows.Add();
                newRow["Employee"] = employee;
                var empMonthGroup = empGroup.GroupBy(mg => new { mg.Year, mg.Month });

                foreach (var empYearMonthGroup in empMonthGroup)
                {
                    int year = empYearMonthGroup.Key.Year;
                    int month = empYearMonthGroup.Key.Month;
                    string colName = string.Format("{0} {1}", dtf.GetMonthName(month), year);
                    if (!tblMonthResultJob1.Columns.Contains(colName))
                        tblMonthResultJob1.Columns.Add(colName, typeof(int));
                    int empJob1Count = empYearMonthGroup.Sum(x => x.Row.Field<int>("Job1"));
                    newRow[colName] = empJob1Count;
                }
            }
在这一行:

int empJob1Count = empYearMonthGroup.Sum(x => x.Row.Field<int>("Job1"));
I am getting error: {System.InvalidCastException: Cannot cast DBNull.Value to type 'System.int'. Please use a nullable type. 

有人能建议如何解决这个问题吗。

从数据库返回的基础值为NULL,不能存储在int中

请改用可为null的int

int empJob1Count = empYearMonthGroup.Sum(x => x.Row.Field<int?>("Job1") ?? 0);
编辑

@菲尔说得很对。请参阅空合并运算符的使用。当基础值为null时,它将使用0,这对求和没有影响。

检查此项

 int empJob1Count = empYearMonthGroup.Where(x => x.Row["Job1"] != DBNull.value).Sum(x=>x.Row.Field<int>("Job1"));

当我写这行时,这不起作用:int-empJob1Count=empYearMonthGroup.Sumx=>x.Row.FieldJob1;我得到错误:无法将类型“int”隐式转换为“int”。存在显式转换。是否缺少强制转换?所以我使用了这行代码:int-empJob1Count=Convert.ToIntempYearMonthGroup.Sumx=>x.Row.FieldJob1;仍然得到相同的错误。它应该是x=>x.Row.FieldJob1??0Thanks@Phil。我想知道Sum是否足够聪明,可以自动忽略空值。显然不是没有Abi,这也不行。错误无法将带[]的索引应用于“AnonymousType1'-x[Job1]类型的表达式
 int empJob1Count = empYearMonthGroup.Where(x => x.Row["Job1"] != DBNull.value).Sum(x=>x.Row.Field<int>("Job1"));