Reputation: 75
I am trying to display email in combobox
by filtering using email. My problem is that my data is encrypted in the users table.
When I am trying to decrypt it it giving this error:
LINQ to Entities does not recognize the method 'System.String Decrypt(System.String, System.String)' method, and this method cannot be translated into a store expression
How can I solve this error?
Here is my Lookup class
public class Lookup
{
public long boundvalue { get; set; }
public string boundtext { get; set; }
}
Here is my code to filter
public IEnumerable<Lookup> getUser(string fText)
{
var ret = new List<Lookup>
{
new Lookup
{
boundvalue = 0,
boundtext = ""
}
};
if (!string.IsNullOrEmpty(fText))
{
ret.AddRange(_entities.Users.Where(x =>EncDec.Decrypt(x.UserVar01.Trim().Replace("_",string.Empty),
Enums.EncDecSecKeyToString(Enums.EncDecSecKey.Email)).Contains(fText.Trim()))
.Select(select => new Lookup
{
boundvalue = select.UserID,
boundtext = EncDec.Decrypt(select.UserVar01.Trim().Replace("_", string.Empty),
Enums.EncDecSecKeyToString(Enums.EncDecSecKey.Email)),
}));
}
return ret;
}
Upvotes: 2
Views: 1409
Reputation: 75
I have managed to get a solution it was just a little problem.
I was suppose to include ToList()
ret.AddRange(_entities.Users.ToList().Where(x =>EncDec.Decrypt(x.UserVar01.Trim().Replace("_",string.Empty),
Enums.EncDecSecKeyToString(Enums.EncDecSecKey.Email)).Contains(fText.Trim()))
.Select(select => new Lookup
{
boundvalue = select.UserID,
boundtext = EncDec.Decrypt(select.UserVar01.Trim().Replace("_", string.Empty),
Enums.EncDecSecKeyToString(Enums.EncDecSecKey.Email)),
}));
Now it working.
Upvotes: 0
Reputation: 39326
Remember a Linq to Entities query at the end is translated to sql notation but what the error is saying is System.String Decrypt
is not supported at the time to do that translation. That's why I'm afraid you can't apply the filter in the server side, you will need to do it in memory. To do that use AsEnumerable
extension method to make the switch to Linq to Objects
ret.AddRange(_entities.Users.AsEnumerable().Where(x =>EncDec.Decrypt(x.UserVar01.Trim().Replace("_",string.Empty),
Enums.EncDecSecKeyToString(Enums.EncDecSecKey.Email)).Contains(fText.Trim()))
.Select(select => new Lookup
{
boundvalue = select.UserID,
boundtext = EncDec.Decrypt(select.UserVar01.Trim().Replace("_", string.Empty),
Enums.EncDecSecKeyToString(Enums.EncDecSecKey.Email)),
}));
Upvotes: 2