简体   繁体   中英

linq to entities where condition issue

I am trying to list all the rows that satisfies either condition # 1 or #2 and returning both rows for #1 & #2. but the problem is returning only rows that satisfies one condition (#1 or #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();

Appreciate any help..

You want condition 1 and not condition 2 -> (condition1) && !(condition2)

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();

Try taking the first "&&" after the where clause, and the unnecesary brackets

    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();

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM