linq到条件问题的实体

时间:2015-08-04 00:36:53

标签: c# linq linq-to-entities

我试图列出满足条件#1或#2的所有行,并返回#1和#2的两行。 #2。但问题是只返回满足一个条件(#1或#2)的行。

    var query = (from c in context.Tasks where 
                && ((c.FK_PrivacyID == 1 && c.Fk_TaskFollowerTypeID == 1)  
                 || (c.TaskFollower == FK_userID && c.Fk_TaskFollowerTypeID == 2))
                  orderby c.CreatedDate descending
                  orderby c.LastModificationDate descending 
                  select c)).ToList();

感谢任何帮助..

2 个答案:

答案 0 :(得分:1)

您想要条件1而不是条件2 - > (条件1)&& !(条件2)

var query = (from c in context.Tasks where 
                ((c.FK_PrivacyID == 1 && c.Fk_TaskFollowerTypeID == 1)  
                 && !(c.TaskFollower == FK_userID && c.Fk_TaskFollowerTypeID == 2))
                  orderby c.CreatedDate descending
                  orderby c.LastModificationDate descending 
                  select c).ToList();

答案 1 :(得分:1)

尝试第一个“&&”在where子句之后,以及不必要的括号

    var query = (from c in context.Tasks where 
            (c.FK_PrivacyID == 1 && c.Fk_TaskFollowerTypeID == 1)  
             || (c.TaskFollower == FK_userID && c.Fk_TaskFollowerTypeID == 2)
              orderby c.CreatedDate descending
              orderby c.LastModificationDate descending 
              select c)).ToList();