0

我在 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:

Employee      January       February            March
A             9             5                   4
B             6             7
C             6             2
D             7

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. 

任何人都可以建议如何解决这个问题。

4

2 回答 2

1

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

请改用可为空的 int。

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

编辑

@Phil 非常正确。请参阅 null 合并运算符的使用。当基础值为 null 时,它将使用 0 代替(这对您的 Sum 没有影响)。

于 2013-02-25T11:17:09.653 回答
0

检查这个

 int empJob1Count = empYearMonthGroup.Where(x => x.Row["Job1"] != DBNull.value).Sum(x=>x.Row.Field<int>("Job1"));
于 2013-02-25T11:30:25.990 回答