3

I have the following Linq to SQL query.

var users = rdc.Users.Where(u => u.EmailAddress.Equals(emailaddress) && password.Equals(hash));

But it always translates to:

SELECT [t0].[Id], [t0].[Guid], [t0].[RoleId], [t0].[EmailAddress], [t0].[Password], [t0].[FirstName], [t0].[LastName], [t0].[Created], [t0].[Activated], [t0].[LastAction]
FROM [dbo].[Users] AS [t0]
WHERE 0 = 1

I just re-imported the tables into my DBML file to insure that the latest schema is reflected, but it still removes my clause and adds WHERE 0 = 1

I can use ExecuteQuery<T>(string, params[]) but I want to find the root cause of this.

1
  • Please show the types of all relevant fields and variables.
    – Mark Byers
    Dec 3, 2012 at 22:40

2 Answers 2

6

Are hash and password defined above this line? It looks like you might have forgetten to add the u. in your query. If password was already defined, then you wouldn't see an error, but it would never equal hash.

Did you mean to write:

var users = rdc.Users.Where(u => u.EmailAddress == emailaddress && u.Password == hash);
                                                                   ^^^

Also, @Mark pointed out that using the == operator will show errors that the .Equals operator might ignore.

1
  • 1
    +1 Good spot. Even if this doesn't fully solve the problem, the query should definitely be rewritten.
    – Mark Byers
    Dec 3, 2012 at 22:44
1

For those, who also looking for solution, EF can set query filter on WHERE 0 = 1 in case if your subquery always returns empty result. Maybe this is some kind of optimisation. So, look closely on your subquery. Some expression there always turns it to empty result.

Example:

 return clientIds == null ? result : result.Where(r => ...);

Here, in subquery method return, if clientIds is List<T>, then expression will filter all results because list is always not null, even if it has no elements. The correct way is like:

 return clientIds?.Count < 1 ? result : result.Where(r => ...);

Your Answer

Reminder: Answers generated by Artificial Intelligence tools are not allowed on Stack Overflow. Learn more

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.