UnitOfWork嵌套查询

本文关键字:查询 嵌套 UnitOfWork | 更新日期: 2023-09-27 18:01:19

我正在尝试转换我的旧查询以适应我的工作单元存储库模式。

它不喜欢我的嵌套查询并产生An expression tree may not contain a call or invocation that uses optional arguments

原始查询

Clients = (from  z in ctx.Interactions
           where !z.Attendees
               .Any(y => ctx.LoanParties
                   .Any(party => party.Person_Id == y.Person.Id && 
                         select z).Count();

工作单位查询

Clients  = UnitOfWork.InteractionRepository.Get(
                z => !z.Attendees.Any(
                        y =>
                            !(UnitOfWork.LoanPartyRepository.Get(
                                party =>
                                    party.Person_Id == y.Person.Id).Any())))
                .Select(z => z)
                .AsParallel()
                .Count();

Intellisense不喜欢嵌套工作单元查询末尾的.Any()。请问使用工作单元进行嵌套查询的正确语法是什么?

UnitOfWork嵌套查询

您可能只是缺少了一些引用。

然而,如果你想正确地做到这一点,你应该封装整个逻辑在你的存储库中(为了在IQueryable而不是IEnumerable上工作)。这样查询将由数据库处理,并且会更快。它也是可重复使用的。

  1. InteractionRepository中创建GetWithSpecificCondition
  2. 移动尽可能多的代码形式,你可以剪切
  3. 不要在内部使用LoanPartyRepository -只需在DbContext上工作