Hanouf D.
Hanouf D.

Reputation: 49

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..

Upvotes: 0

Views: 44

Answers (2)

jmd1921
jmd1921

Reputation: 46

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

Upvotes: 1

Frank Ibem
Frank Ibem

Reputation: 834

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

Upvotes: 1

Related Questions