无法从IQueryable <decimal,project1,model>转换为IQueryable <project1,model>

时间:2015-06-19 15:29:13

标签: c# .net linq

这个错误是什么意思?

我的错误是:

Cannot implicitly convert type 'System.Linq.IQueryable<System.Linq.IGrouping<decimal,Project1.Domain.Models.DepartmentBreakdownReport>>' to 'System.Linq.IQueryable<Project1.Domain.Models.DepartmentBreakdownReport>'. An explicit conversion exists (are you missing a cast?)

我想知道它是什么,以便我能够解决它。它只发生在我的结果上添加LINQ GroupBy时。

 public IQueryable<DepartmentBreakdownReport> GetDepartmentBreakdownBySupplierIDAndReviewID(int ClientID, int? SupplierID, int? ReviewID) {
            return (from d in Db.Details
                    join h in Db.Headers
                    on new { d.ClientID, d.ClaimID }
                    equals new { h.ClientID, h.ClaimID }
                    where d.ClientID == ClientID && h.SupplierID == SupplierID

                    join sd in Db.SuppDepts
                    on new { a = d.ClientID, b = d.CategoryID ?? 0 }
                    equals new { a = sd.ClientID, b = sd.CategoryID }

                    join r in Db.Reviews
                    on new { h.ClientID, h.ReviewID }
                    equals new { r.ClientID, r.ReviewID }

                    join rp in Db.ReviewPeriods
                    on new { a = r.ClientID, b = r.ReviewPeriodID ?? 0 }
                    equals new { a = rp.ClientID, b = rp.ReviewPeriodID }
                    where r.ReviewID == ReviewID

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

                    select new DepartmentBreakdownReport {
                        DepartmentName = sd.DepartmentName,
                        SumOfAmount = d.Amount,
                        SupplierID = h.SupplierID,
                        ReviewID = h.ReviewID,
                        ReviewName = rp.ReviewPeriodName,
                        SupplierName = su.SupplierName,
                        ClientID = d.ClientID
                    }).GroupBy(r=>r.Amount);
        }

2 个答案:

答案 0 :(得分:0)

  

这个错误是什么意思?

您的返回类型为IQueryable<DepartmentBreakdownReport>,但您尝试返回IGrouping<decimal,Project1.Domain.Models.DepartmentBreakdownReport>

当您致电.GroupBy时,您将返回类型更改为IGrouping而非IQueryable,它还会将小数作为IGrouping表达式的一部分引入(这是您要分组的金额变量通过)。

要解决此问题,您只需将方法签名更改为:

即可
public IGrouping<decimal,Project1.Domain.Models.DepartmentBreakdownReport> GetDepartmentBreakdownBySupplierIDAndReviewID(int ClientID, int? SupplierID, int? ReviewID)

答案 1 :(得分:0)

对我而言,方法名称GetDepartmentBreakdownBySupplierIDAndReviewID意味着:

  • 首先,方法返回类型是这样的,假设SupplierIdReviewId的int类型:

IGrouping<Tuple<int, int>, IQueryable<DepartmentBreakdownReport>> 
                                       GetDepartmentBreakdownBySupplierIDAndReviewID(...)
  • 其次,返回应该是最终投影,例如:

select new DepartmentBreakdownReport { ... })
    .GroupBy(r => new Tuple<int, int>(r.SupplierID, r.ReviewID));