2

I have a function to retrive the user detail

I have encryped few fields. I want to know how the LINQ will execute the actual sql query

public User GetUserByEmail(string email)
{
      return _db.Users.Where(x => x.LastName.Decrypt() == "Patel").ToList();
}

x.LastName.Decrypt() will it fetch all records from sql and perform decrypt on each field in the code side ?

What if i use

public User GetUserByEmail(string email)
{
      return _db.Users.Where(x => x.LastName == "Patel".Encrypt()).ToList();
}

which one is better

1
  • Please provide Decrypt definition.
    – Andrey
    Sep 24, 2013 at 13:06

2 Answers 2

1

Good chances are that your first query isn't going to work at all, unless you force bringing the data into the memory by calling AsEnumerable(), ToList, or ToArray, because EF provider does not know how to translate your Encrypt function to SQL.

The second query, on the other hand, should work fine, because it's a string-to-string comparison.

In addition, the second way of querying lets you implement a potentially more secure scheme when "encrypting" a string cannot be reversed with a Decrypt, e.g. when you store a message digest.

1
  • you are right @dasblinkenlight, the first query will result in a runtime error Sep 26, 2013 at 6:08
0

Calling .Decrypt() in the Where function will decrypt the LastName on each & every row. You're better off with the .Encrypt() method you displayed, which will call 'Encrypt` once and compare each LastName to the encrypted string for "Pate1".

Your Answer

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

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