0

I am have an issue with a linq query. I am joining two tables but the where clauses are being completely ignored.

using (var db = new Context())
{
    var count = (from c in db.PERSON
                 join dt in db.DATA_INPUT_CHANGE_LOG
                      on c.DataInputTypeId equals dt.DataInputTypeId
                 join x in db.DATA_INPUT_CHANGE_LOG 
                      on c.Id equals x.DataItemId
                 where c.PersonId == p1Id &&
                  c.RefPersonId == p2Id && 
                  c.RelationshipId == rId
                 where x.Approved
                 where x.Checked
                 where x.Hidden == false
                 select c).Count();

    return count > 0;
}

In this particular query the x.Approved, x.Checked and x.Hidden == false where clauses are completely ignored.

Can anyone point me in the right direction?

HaveNoDisplayName
  • 8,291
  • 106
  • 37
  • 47
Tommy
  • 445
  • 1
  • 6
  • 15
  • 4
    Take a look here: http://stackoverflow.com/questions/679644/multiple-where-clause-in-linq You probably want to specify where once – Michael B Jun 11 '15 at 12:20
  • 2
    Have you run a profiler against your database to see exactly what sql is generated from that LINQ? – slugster Jun 11 '15 at 12:29
  • 3
    Why are you using multiple where's instead of just one and AND's? – Gusman Jun 11 '15 at 12:30

1 Answers1

-1

Your syntax is incorrect. You should only have one where clause. See below:

            var count = (from c in db.PERSON
                         join dt in db.DATA_INPUT_CHANGE_LOG
                              on c.DataInputTypeId equals dt.DataInputTypeId
                         join x in db.DATA_INPUT_CHANGE_LOG
                              on c.Id equals x.DataItemId
                         where c.PersonId == p1Id &&
                          c.RefPersonId == p2Id &&
                          c.RelationshipId == rId &&
                          x.Approved &&
                          x.Checked &&
                          x.Hidden == false
                         select c).Count();

            return count > 0;
Mike
  • 550
  • 2
  • 16
  • The two options are equivalent. There is nothing *wrong* with multiple `where` clauses. They'll still work just fine. – Servy Jun 11 '15 at 13:33