【问题标题】:Linq query for search entities with many-to-many relationships具有多对多关系的搜索实体的 Linq 查询
【发布时间】:2012-02-19 12:55:16
【问题描述】:

我在搜索时遇到问题。 所以,我有任务实体和所有者实体。他们处于多对多关系中。 我的目标 - 找到某个所有者的所有任务。在实体框架数据模型中,没有关系表作为优秀实体。 我创建了 linq-filter 表达式,但我不能在其中包含所有者的规则。我尝试使用一些 lambda 表达式,但编译器要求我使用简单类型并且没有“所有者”。

            Expression<Func<Task, bool>> filter = e =>
                (String.IsNullOrEmpty(filterData.Title) || e.Title.StartsWith(filterData.Title))
               && (isDueDateSkipped || (DateTime.Compare(dueDate, e.DueDate ?? now) == 0))                       
               && (isCloseDateSkipped || (DateTime.Compare(closeDate, e.CloseDate ?? now) == 0))
               && (isTypeSkipped || e.Type.Id == typeId)
               && (isStatusSkipped || e.Status.Id == statusId)
               && (isPrioritySkipped || e.Priority.Id == priorityId)
               && (isMemberSkipped || e.Member.Id == memberId);

你能帮帮我吗?

【问题讨论】:

    标签: .net linq entity-framework linq-to-entities


    【解决方案1】:

    我假设 e 是一个任务:

     Expression<Func<Task, bool>> filter = e =>
                (String.IsNullOrEmpty(filterData.Title) || e.Title.StartsWith(filterData.Title))
               && (isDueDateSkipped || (DateTime.Compare(dueDate, e.DueDate ?? now) == 0))                       
               && (isCloseDateSkipped || (DateTime.Compare(closeDate, e.CloseDate ?? now) == 0))
               && (isTypeSkipped || e.Type.Id == typeId)
               && (isStatusSkipped || e.Status.Id == statusId)
               && (isPrioritySkipped || e.Priority.Id == priorityId)
               && (isMemberSkipped || e.Member.Id == memberId)
               && (isOwnerSkipped || TaskOwners.Where(to => to.TaskID == e.TaskID && to.OwnerID == ownerFilter.OwnerID).Count() > 0);
    

    希望对你有帮助

    【讨论】:

    • 我有一个错误:LINQ to Entities 不支持指定的类型成员“TaskOwners”。
    • 那是你的多对多关系表的实体名称吗?我给你的主要是一个草图,你可能需要适应你的代码。
    【解决方案2】:

    我找到了问题的答案:

      Expression<Func<Task, bool>> filter = e =>
                       (String.IsNullOrEmpty(filterData.Title) || e.Title.StartsWith(filterData.Title))
                       && (isDueDateSkipped || (DateTime.Compare(dueDate, e.DueDate ?? now) == 0))
                       && (isCloseDateSkipped || (DateTime.Compare(closeDate, e.CloseDate ?? now) == 0))
                       && (isTypeSkipped || e.Type.Id == typeId)
                       && (isStatusSkipped || e.Status.Id == statusId)
                       && (isPrioritySkipped || e.Priority.Id == priorityId)
                       && (isMemberSkipped || e.Member.Id == memberId)
                       && (isOwnerSkipped || e.Owners.Select(o => o.Id).Contains(ownerId));
            return filter;
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2016-12-15
      • 1970-01-01
      • 2017-10-20
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多