使 LINQ 投影通用

Making the LINQ projection generic

我有一个 LINQ,它访问 table 一个大数据集,所以这个查询对性能很关键。我还希望对过滤后的数据集进行动态排序,因此我使用 LINQ 扩展通过在运行时传递排序列来进行排序。

由于遇到性能问题,我正在做排序后的数据投影(下面给出查询)。因此,我必须在 OrderBy 和 OrderByDescending 扩展中重复投影。有没有办法提取投影以减少重复代码?

这是我的代码: 从源过滤数据 table -

    var allOrderEntries = (from l in context.Orders
            where (branches.Count == 0 || branches.Contains(l.BranchId)) 
                && l.IsActive && !l.IsArchived 
                && (l.PreferredLanguage.LanguageKey == languageKey string.IsNullOrEmpty(languageKey))
                && (string.IsNullOrEmpty(searchText) || (l.FirstName == searchText || l.LastName == searchText || l.Notes == searchText || l.AccountNumber == searchText))
            select l;

这 returns 一个 IQueryable,然后我将其传递给 OrderBy 扩展。这是我要解决的投影重复问题。

if (sortInfo.SortDirection == Common.Enum.SortDirection.Ascending)
                orderedList = allOrderEntries.OrderBy(sortInfo.SortColumn).Skip(pageInfo.Skip).Take(pageInfo.PageSize).Select(data => new OrderEntryDetailsDto
                {
                    Id = data.OrderId.ToString(),
                    AccountNumber = accountNumber.Mask(data.AccountNumber),
                    FirstName = data.FirstName,
                    LastName = data.LastName,
                    ServicesRequested = data.ServicesRequested, //string.Join(",", data.ServicesRequested.Select(s=> s.ServiceName).ToArray()),
                    PersonRequested = data.UserRequested != null ? data.UserRequested.FirstName + " " + data.UserRequested.LastName : data.GroupRequested != null ? data.GroupRequested.GroupName : string.Empty,
                    Notes = data.Notes ?? string.Empty,
                    WaitTime = string.Format("{0:hh\:mm\:ss}", (TimeSpan)(DateTime.UtcNow - data.AddedTime)),
                    Type = data.FkAppointmentId == null ? "Walk-In" : (from a in context.Appointments where a.AppointmentId == data.FkAppointmentId select a).Single().AppointmentStatus.AppointmentStatusName.ToString()           
                }).ToList();
            else
                orderedList = allOrderEntries.OrderByDescending(sortInfo.SortColumn).Skip(pageInfo.Skip).Take(pageInfo.PageSize).Select(data => new OrderEntryDetailsDto
                {
                    Id = data.OrderId.ToString(),
                    AccountNumber = accountNumber.Mask(data.AccountNumber),
                    FirstName = data.FirstName,
                    LastName = data.LastName,
                    ServicesRequested = data.ServicesRequested, //string.Join(",", data.ServicesRequested.Select(s=> s.ServiceName).ToArray()),
                    PersonRequested = data.UserRequested != null ? data.UserRequested.FirstName + " " + data.UserRequested.LastName : data.GroupRequested != null ? data.GroupRequested.GroupName : string.Empty,
                    Notes = data.Notes ?? string.Empty,
                    WaitTime = string.Format("{0:hh\:mm\:ss}", (TimeSpan)(DateTime.UtcNow - data.AddedTime)),
                    Type = data.FkAppointmentId == null ? "Walk-In" : (from a in context.Appointments where a.AppointmentId == data.FkAppointmentId select a).Single().AppointmentStatus.AppointmentStatusName.ToString()   
                }).ToList();

所以我想重做 Select 部分,这是重复的,最好的方法是什么?

试试这个,因为它是 IQueryable

if (sortInfo.SortDirection == Common.Enum.SortDirection.Ascending)
{     
    allOrderEntries = allOrderEntries.OrderBy(sortInfo.SortColumn)
                     .Skip(pageInfo.Skip).Take(pageInfo.PageSize).AsQueryable();
}
else
{
    allOrderEntries = allOrderEntries.OrderByDescending(sortInfo.SortColumn)
                     .Skip(pageInfo.Skip).Take(pageInfo.PageSize).AsQueryable();
}


var orderedList = allOrderEntries.Select(data => new OrderEntryDetailsDto
            {
                Id = data.OrderId.ToString(),
                AccountNumber = accountNumber.Mask(data.AccountNumber),
                FirstName = data.FirstName,
                LastName = data.LastName,
                ServicesRequested = data.ServicesRequested, //string.Join(",", data.ServicesRequested.Select(s=> s.ServiceName).ToArray()),
                PersonRequested = data.UserRequested != null ? data.UserRequested.FirstName + " " + data.UserRequested.LastName : data.GroupRequested != null ? data.GroupRequested.GroupName : string.Empty,
                Notes = data.Notes ?? string.Empty,
                WaitTime = string.Format("{0:hh\:mm\:ss}", (TimeSpan)(DateTime.UtcNow - data.AddedTime)),
                Type = data.FkAppointmentId == null ? "Walk-In" : (from a in context.Appointments where a.AppointmentId == data.FkAppointmentId select a).Single().AppointmentStatus.AppointmentStatusName.ToString()   
            }).ToList();