简体   繁体   English

LINQ 表达式无法翻译

[英]LINQ Expression could not be translated

I have migrated the application from .net 3.1 to 6 and it shows the error in LINQ queries.我已将应用程序从 .net 3.1 迁移到 6,它在 LINQ 查询中显示错误。 I have resolved others but can't figure out the issue in this one.我已经解决了其他人,但无法弄清楚这个问题。

Controller: Controller:

public async Task<IActionResult> Index(Guid? contractId, DateTime? startDate, DateTime? endDate)
{
    if (!contractId.HasValue)
        return View(new ReportDataViewModel());

    var svm = new ReportDataViewModel()
    {
        ContractId = contractId                
    };

    if (startDate.HasValue || endDate.HasValue)
    {
        svm.StartDate = startDate;
        svm.EndDate = endDate;
    }
**  var result = await _reportServices.GetExpiredBankAccounts(svm);
**  return View(result);
    
}

Service:服务:

public async Task<ReportDataViewModel> GetExpiredBankAccounts(ReportDataViewModel model)
{
    var baseQuery = _bankAccountValidationServices.GetAllExpiredBankAccounts();
    
    if (model.ContractId != null)
    {
        baseQuery = baseQuery.Where(x => x.ContractId == model.ContractId);
    }

    if (model.StartDate != null)
    {
        baseQuery = baseQuery.Where(x => x.OrganisationUnit.ClaimantBankAccounts.Any(y =>
                    y.DateLastReminderSentOpenClaim.Value >= model.StartDate || y.DateLastReminderSentUnOpenedClaim >= model.StartDate));
    }
    if (model.EndDate != null)
    {
        baseQuery = baseQuery.Where(x => x.OrganisationUnit.ClaimantBankAccounts.Any(y =>
                    y.DateLastReminderSentOpenClaim.Value <= model.EndDate || y.DateLastReminderSentUnOpenedClaim <= model.EndDate));
    }

**  var data =  baseQuery.GroupBy(x=>x.OrganisationUnit);
**  var result = await data.ToListAsync();

    if (result != null && result.Any())
    {
        model.OrganisationUnits = new List<OrganisationUnit>();
        foreach (var item in result)
        {
            model.OrganisationUnits.Add(item.Key);
        }
        }

    return model;
}

Service:服务:

public IQueryable<OutboundClaim> GetAllExpiredBankAccounts()
{
    var baseQuery =
        _context.OutboundClaims
            .Include(x => x.OrganisationUnit).ThenInclude(x => x.Contract)
            .Include(x => x.OrganisationUnit).ThenInclude(x => x.Address)
            .Include(x => x.OrganisationUnit).ThenInclude(x => x.Contacts)
            .Include(x => x.OrganisationUnit).ThenInclude(x => x.ClaimantBankAccounts).AsQueryable();

    baseQuery = baseQuery.Where(x => x.OrganisationUnit.ClaimantBankAccounts.Any(y => y.Active && !y.IsDeleted));
    return baseQuery;
}

It shows error in the highlighted parts of code.它在代码的突出显示部分显示错误。 It may occur due to multiple queries but I am not share.它可能由于多个查询而发生,但我不分享。

Try the following query:尝试以下查询:

var unitQuery = baseQuery
    .Select(x => x.OrganisationUnit)
    .GroupBy(u => u.Id)
    .Select(g => g.First());

model.OrganisationUnits = await unitQuery.ToListAsync();

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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