c# - 转换为值类型 'System.Int32' 失败,因为具体化值为空。

标签 c# linq

不过,我正在尝试返回结果列表。每当没有结果时,我都会收到上面发布的错误消息。然而,这很奇怪,因为每当我添加变量 q 而不是 return 时,它只是不返回任何结果并且对此没有问题。我更愿意按照我现在正在做的方式去做,有人知道查询有什么问题吗?每当我在 LINQPad 中运行它时,它都可以正常工作。

  public IQueryable<ClaimNumberReport> GetClaimsByClaimNumber(int ClientID, int ClaimID) {
                /*var q = */ return (from d in camOnlineDb.Details
                        join a in camOnlineDb.Areas
                            on new { a = d.ClientID, b = d.AreaID ?? 0 }
                        equals new { a = a.ClientID, b = a.AreaID }
                        where d.ClientID == ClientID

                        join r in camOnlineDb.Reasons
                            on new { a = d.ClientID, b = d.ReasonID ?? 0 }
                        equals new { a = r.ClientID, b = r.ReasonID }

                        join sd in camOnlineDb.SuppDepts
                          on new { a = d.ClientID, b = d.CategoryID ?? 0 }
                      equals new { a = sd.ClientID, b = sd.CategoryID } into sdd
                      from sd in sdd.DefaultIfEmpty()

                        join h in camOnlineDb.Headers
                            on new { d.ClientID, d.ClaimID}
                        equals new { h.ClientID, h.ClaimID }
                        where h.ClaimID == ClaimID

                        join su in camOnlineDb.Suppliers
                            on new { h.ClientID, h.SupplierID }
                        equals new {su.ClientID, su.SupplierID }

                        join cp in camOnlineDb.ClaimPacks
                            on new { h.ClientID, h.ClaimID }
                        equals new { cp.ClientID, cp.ClaimID }

                        join rev in camOnlineDb.Reviews
                            on new { h.ClientID, h.ReviewID }
                        equals new { rev.ClientID, rev.ReviewID }

                        join revp in camOnlineDb.ReviewPeriods
                            on new { a = rev.ClientID, b = rev.ReviewPeriodID ?? 0 }
                        equals new { a = revp.ClientID, b = revp.ReviewPeriodID }

                        join st in camOnlineDb.Statuses
                            on new { a = d.ClientID, b = d.StatusID ?? 0 }
                        equals new { a = st.ClientID, b = st.StatusID }

                        join stcm in camOnlineDb.StatusCategoryMappings
                            on new { st.ClientID, st.StatusID }
                        equals new { stcm.ClientID, stcm.StatusID }

                        join stc in camOnlineDb.StatusCategories
                            on new { stcm.StatusCategoryID }
                        equals new { stc.StatusCategoryID }
                        where stc.StatusCategoryTypeID == 1

                        select new ClaimNumberReport {
                            TypeID = d.ClaimTypeID,
                            CPAttached = cp.FileName,
                            ReviewPeriodName = revp.ReviewPeriodName,
                            ClaimID = d.ClaimID,
                            Line = d.ClaimLine,
                            AccountNo = su.AccountNo,
                            SupplierName = su.SupplierName,
                            Amount = d.Amount,
                            Status = st.StatusDesc,
                            DateSent = d.DateSent,
                            DayOS = d.DaysOS,
                            NominalPeriod = d.NominalPeriod,
                            SLInvoiceNo = d.SLInvoiceNo,
                            Area = a.AreaDesc,                   
                            DebitRef = d.DebitFile,
                            DebitDate = d.JournalDate,
                            DeductDate = d.DeductDate,
                            StatusCategoryID = stc.StatusCategoryID,
                            StatusCategoryDesc = stc.StatusCategoryDesc,
                            APLReason = r.ReasonDesc,
                            ClientID = d.ClientID,
                            DeptNo = sd.DepartmentID,
                            DeptName = sd.DepartmentName,
                            Agreed = d.Agreed
                        });
                /*return q;*/
            }

最佳答案

此错误是由于查询结果类型具有非可空类型的列/属性但生成的查询结果为 NULL 值的情况引起的。

这可以被认为是错误或不是。很难看出 L2S 团队应该在这里做些什么不同的事情。我认为他们应该添加更好的错误消息。这个错误是阴险的,因为它有时只会在异常数据下出现在生产中......

您的左连接 (sd) 似乎不匹配,您选择的 sd.* 属性之一必须是 int。像这样解决:

 DeptNo = (int?)sd.DepartmentID, //Cast to nullable

d.CategoryID ?? 0

你在这干什么?这似乎是一种使连接编译的方法。最好使用:

                    join r in camOnlineDb.Reasons
                        on new { a = d.ClientID, b = (int?)d.ReasonID }
                    equals new { a = r.ClientID, b = (int?)r.ReasonID }

此转换使匿名类型签名兼容。生成的 SQL 现在应该更快了。如果你说 x ?? 0 转换为 COALESCE(x, 0) 可以防止使用索引等。

关于c# - 转换为值类型 'System.Int32' 失败,因为具体化值为空。,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31135522/

相关文章:

c# - 如何在列表相同的 Dictionary<string, List<int>> 中按 List<int> 分组?

c# - 无法设置数据库初始值设定项 Entity Framework

c# - 在类级别声明 [XmlElement(IsNullable = true)]

c# - 如何将要在某些 XElement 上执行的操作作为参数传递?

c# - 类型 : 'MySql.Data.Entity.MySqlEFConfiguration' 违反的继承安全规则

c# - 检查二维数组中的值

c# - 按顺序访问使用 LINQ 的 .Skip() 跳过的项目

C# 过滤器列表删除任何双对象

c# - 将参数从一种方法传递到 button.click 的另一种方法

c# - ListView(WPF)中的分隔符?