简体   繁体   中英

LINQ to Entities does not recognize the method 'System.String Decrypt(System.String, System.String)' method

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;
            }

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)),
                             }));

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM