linq join导致非常慢的性能/正确的方式来优化它

时间:2015-03-11 09:43:16

标签: c# performance linq entity-framework linq-to-sql

我已加入linq,如下所示

IEnumerable < UserATrailViewModel > v1 = from u in useratrailbusiness.GetAllUsers().AsQueryable()
join u1 in userPackageAtrailBusiness.GetAllUsers().AsQueryable()
on u.Uid equals u1.Uid into t1
from subpet in t1.DefaultIfEmpty()
orderby u.CreatedDate descending

select new UserATrailViewModel {

    UserATID = u.UserATID,
    Uid = u.Uid,
    RoleId = u.RoleId,

    FirstName = Md5Decryption.Decrypt(u.FirstName),
    LastName = Md5Decryption.Decrypt(u.LastName),
    Email = u.Email,
    UserName = u.UserName,
    CreatedDate = u.CreatedDate,
    IsActive = u.IsActive,
    CreatedBy = u.CreatedBy,
    ModifiedDate = u.ModifiedDate,
    ModifiedBy = u.ModifiedBy,
    //UniqueGuid = u.UniqueGuid,
    CompanyName = Md5Decryption.Decrypt(u.CompanyName),
    Country = Md5Decryption.Decrypt(u.Country),

    State = Md5Decryption.Decrypt(u.State),
    City = Md5Decryption.Decrypt(u.City),

    Address1 = Md5Decryption.Decrypt(u.Address1),
    Address2 = Md5Decryption.Decrypt(u.Address2),
    PhoneNo = Md5Decryption.Decrypt(u.PhoneNo),
    MobileNo = Md5Decryption.Decrypt(u.MobileNo),
    SrvDTStamp = u.SrvDTStamp,
    ClientCountry = u.ClientCountry,
    App_User = u.App_User,
    Audit_Action = u.Audit_Action,
    FullName = Md5Decryption.Decrypt(u.FirstName) + " " + Md5Decryption.Decrypt(u.LastName),
    ContactNo = Md5Decryption.Decrypt(u.PhoneNo) + " , " + Md5Decryption.Decrypt(u.MobileNo),

    PackageName = (subpet == null ? String.Empty : subpet.PackageName),
};

它非常慢,因为即使是40到50条记录也需要18到24秒才能获取数据。我怎么能让它变得更快,因为它非常慢。我不确定它是否是获取这些数据的正确方法。所以除此之外的任何其他替代方案都可以使这个过程更快。

1 个答案:

答案 0 :(得分:1)

您可以首先将所有对Md5Decryption.Decrypt()方法的调用移动到UserATrailViewModel类,无论是在构造函数中还是在自定义setter中。这将使您的查询更简单,因为您不希望数据库进行解密。