Jonathan Roberts
Jonathan Roberts

Reputation: 43

Linq - Entity Framework - Decrypt values into an object using AsQueryable()

I am querying Entity Framework using AsQueryable as I don't want it to go into memory yet as there are a lot of records that it will bring back. But I need to decrypt certain values when selecting the values into an object.

If I were to use ToList(), the code first then decrypt it after in a foreach loop, it works - but it is slow.

Here is some of my code:

var myList = db.Accounts.Include("ProfileDetails").AsNoTracking().Where(x => !x.IsDeleted &&  x.ProfileDetails.IC_Garage.Where(u=>u.Id == garage.Id).Any())
                    .Select(a => new { a.Id, a.RoleId, a.ProfileId, a.Email })
                    .AsQueryable()
                    .Join(db.Profiles
                    .Select(p => new { p.Id, p.FirstName, p.LastName, p.IC_Units, p.EmergencyContact1, p.EmergencyContact2, p.IC_TelephoneBook, p.Salt, p.NextOfKin })
                    .AsQueryable(), profile => profile.ProfileId, userProfile => userProfile.Id, (profile, userProfile) => new { profile, userProfile })
                             .Select(s => new MyGarageList
                             {
                                 EmergencyContact1 = s.userProfile.EmergencyContact1, Salt = s.userProfile.Salt
            }).ToList();

This is the bit I don't really want to do as it slows the process down

List<MyGarageList> filteredGarages = new List<MyGarageList>();

foreach (MyGarageList garage in myList)
{
    string emrgName1 = !string.IsNullOrEmpty(garage.EmergencyContact1) ? EL.DecryptText(garage.EmergencyContact1, garage.Salt, WebsiteKey) : "";
    MyGarageList newMyList = new MyGarageList()
            {
                EmergencyContact1 = emrgName1
            };
    filteredGarages.Add(newMyList);
}

Any ideas on how I can speed up the process? Can I decrypt the values in the first call?

Jon

Upvotes: 2

Views: 91

Answers (0)

Related Questions