【问题标题】:Using the AND (&&) operator on IncludeFilter in Entity Framework Plus not bringing back nested / child object在 Entity Framework Plus 中对 IncludeFilter 使用 AND (&&) 运算符不会带回嵌套/子对象
【发布时间】:2020-09-04 11:05:41
【问题描述】:

我只是在 Entity framework Plus 中找到自己的脚,以带回具有一组复杂约束/要求的数据。我能够成功使用它,但是在 IncludeFilter() 方法中使用 && 运算符时,我无法返回嵌套/子对象。

我有一个 Company 对象 > 每个都有多个 CommunicationLink 对象 > 每个 CommunicationLink 对象都有一个 Communication 对象。我已经确定添加多个“包含过滤器”可用于复制“或”功能(||),但我不能终生使用 AND 运算符来过滤结果并获取嵌套对象通过。下面是示例代码:

示例 1:

//2 WHERE CLAUSES
var companiesData = _dbContext.Companies
     .IncludeFilter(comp => comp.CommunicationLinks
                .Where(cmli =>
                    string.Equals(cmli.Communication.Action, "PhoneOut")
                )
                .Where(cmli => cmli.UserId == comp.AccountManager) 
                .Select(comm => comm) //I believe this is where the problem lies
      )
      .Where(co =>
           string.Equals(co.Type, "Customer")
           && string.Equals(co.Status, "Active")
           && co.Deleted != 1 
       )
       .OrderBy(c => c.Name);

示例 2:

   //USING && INSTEAD OF 2 WHERE CLAUSES
    var companiesData = _dbContext.Companies
         .IncludeFilter(comp => comp.CommunicationLinks
                    .Where(cmli =>
                        string.Equals(cmli.Communication.Action, "PhoneOut")
                        && cmli.UserId == comp.AccountManager 
                    )
                    .Select(comm => comm) //I believe this is where the problem lies
          )
          .Where(co =>
               string.Equals(co.Type, "Customer")
               && string.Equals(co.Status, "Active")
               && co.Deleted != 1 
           )
           .OrderBy(c => c.Name);

我正在尝试过滤结果以仅返回子 Communication.Action 字段为“PhoneOut”且 Communication.UserId 等于 Company.AccountManager (Id) 中的值的 CommunicationLink 记录。过滤后的结果运行良好,但是 Communication 对象(我希望通过 .Select() 方法获得)对于这些记录返回为 null。

【问题讨论】:

    标签: c# sql entity-framework linq entity-framework-plus


    【解决方案1】:

    今天早上在这个问题上挣扎了一段时间后,我解决了。

    //2 WHERE CLAUSES
    var companiesData = _dbContext.Companies
         .IncludeFilter(comp => comp.CommunicationLinks
                    .Where(cmli =>
                        string.Equals(cmli.Communication.Action, "PhoneOut")
                    )
                    .Where(cmli => cmli.UserId == comp.AccountManager) 
                    .Select(comm => comm.Communication) //forgot Communication!!
          )
          .Where(co =>
               string.Equals(co.Type, "Customer")
               && string.Equals(co.Status, "Active")
               && co.Deleted != 1 
           )
           .OrderBy(c => c.Name);
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2021-11-08
      • 2021-01-15
      • 1970-01-01
      • 2018-11-02
      • 1970-01-01
      • 2017-07-13
      • 1970-01-01
      相关资源
      最近更新 更多