简体   繁体   中英

Cannot implicitly convert type 'System.Linq.IQueryable<AnonymousType#1>' to 'System.Linq.IQueryable<>'

public IQueryable<DepartmentBreakdownReport> GetDepartmentBreakdownByReviewID(int ClientID, int? ReviewID) {
    var x = (from d in camOnlineDb.Details
             join h in camOnlineDb.Headers
             on new { d.ClientID, d.ClaimID }
             equals new { h.ClientID, h.ClaimID }
             where h.ClientID == d.ClientID

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

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

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

             select new {
                 d.ClientID,
                 h.ReviewID,
                 sd.DepartmentID,
                 sd.DepartmentName,
                 d.Amount
             });

    x.GroupBy(r => new { r.DepartmentID, r.ReviewID, r.ClientID })
    .Select(g => new {
        ClientID = g.Key.ClientID,
        ReviewID = g.Key.ReviewID,
        Dept = g.Max(d => d.DepartmentName),
        Amount = g.Sum(d => d.Amount)
    })
    .OrderBy(r => r.Dept)
    .Where(r => r.ReviewID == 37);
    //.Dump();

    return x;

I know it has something to do with returning x. I tried returning is asQueryable but this didn't work. How do I get my statement to return x?

x is an IQueryable<T> where the T is the anonymous type with ClientID , ReviewID , DepartmentID , DepartmentName and Amount methods.

At some point you need to do a either a select new DepartmentBreakdownReport(…) , as select new DepartmentBreakdownReport{…} a .Select(something => new DepartmentBreakdownReport(…)) or a .Select(something => new DepartmentBreakdownReport{…}) . (Which are all the same thing really.

That will then give you an IQueryable<DepartmentBreakdownReport> because the type it has selected will be DepartmentBreakdownReport .

Note also that the code x.GroupBy(r => new { r.DepartmentID, r.ReviewID, r.ClientID }) and so on in your code effectively does nothing; it creates a new query, but then that new query is never used or even touched.

What I meant by my earlier comment(s) is that you want to create (and return) instances of DepartmentBreakdownReport , not an anonymous type.

return x.Where(r => r.ReviewID == 37)
        .GroupBy(r => new { r.DepartmentID, r.ReviewID, r.ClientID })
        .Select(g => new DepartmentBreakdownReport
                         {
                             ClientID = g.Key.ClientID,
                             ReviewID = g.Key.ReviewID,
                             Dept = g.Max(d => d.DepartmentName),
                             Amount = g.Sum(d => d.Amount)
                         })
        .OrderBy(r => r.Dept);

I reordered the query a bit to my personal taste, but it should produce the same results.

Also, I don't know if you'll have to add an AsQueryable() on to the end of that or not...

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM