Linq查询具有多对多关系的搜索实体

时间:2012-01-26 12:16:19

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

我有搜索问题。 所以,我有Task实体和Owner Entity。他们处于多对多的关系中。 我的目标 - 由一些所有者查找所有任务。在实体框架数据模型中,没有关系表作为未完成的实体。 我创建了linq-filter表达式,但我不能在其中包含Owners规则。我尝试使用一些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);

你能帮我吗?

2 个答案:

答案 0 :(得分:0)

我认为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);

希望这有帮助

答案 1 :(得分:0)

我找到了问题的答案:

  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;