我有一个已经测试并且正在运行的LINQ查询,但是在将查询结果解析为我的操作结果时。
异常错误: Message =“仅在LINQ to Entities中的排序输入中才支持方法'Skip'。必须在方法'Skip'之前调用方法'OrderBy'。”
问题是我正在使用过滤器来解析orderby参数(即动态orderby参数)。 如何处理Linq的动态orderby语句?
某些其他线程提到在orderby子句中使用lamda表达式,但是该参数是动态的,我不想对sort参数进行硬编码。操作结果和查询如下:
private DataSourceResult GetAssetsByFilter(AssetFilter assetFilter, DataSourceRequest request, AssetContext context)
{
try
{
context.Configuration.LazyLoadingEnabled = false;
//Set Paging Parameters
int pageSize = request.PageSize;
int page = request.Page;
//LINQ Query
var query =
(from a1 in context.Assets
join c1 in context.Containers on a1.ContainerId equals c1.Id
join person in context.Contacts on c1.ContactId equals person.Id
join cat1 in context.Categories on a1.CategoryLevel1Id equals cat1.Id
join cat2 in context.Categories on a1.CategoryLevel2Id equals cat2.Id
into cat2group
from cat2gp in cat2group.DefaultIfEmpty()
join cat3 in context.Categories on a1.CategoryLevel3Id equals cat3.Id
into cat3group
from cat3gp in cat3group.DefaultIfEmpty()
join cat4 in context.Categories on a1.CategoryLevel4Id equals cat4.Id
into cat4group
from cat4gp in cat4group.DefaultIfEmpty()
where (!a1.IsDeleted)
&& (!c1.IsDeleted)
&& c1.ProjectId == SessionHelper.ProjectId
select new
{
a1.Id,
a1.ContainerId,
a1.Barcode,
a1.SerialNumber,
a1.CaptureDate,
a1.ModifiedDate,
a1.AssetOwnerTypeId,
a1.AssetConditionId,
a1.Username,
a1.IsDeleted,
a1.CategoryLevel1Id,
CategoryLevel1Text = cat1.Text,
CategoryLevel2Id = (cat2gp == null ? Guid.Empty : cat2gp.Id),
CategoryLevel2Text = (cat2gp == null ? String.Empty : cat2gp.Text),
CategoryLevel3Id = (cat3gp == null ? Guid.Empty : cat3gp.Id),
CategoryLevel3Text = (cat3gp == null ? String.Empty : cat3gp.Text),
CategoryLevel4Id = (cat4gp == null ? Guid.Empty : cat4gp.Id),
CategoryLevel4Text = (cat4gp == null ? String.Empty : cat4gp.Text),
c1.ContactId,
person.Name,
c1.LocationLevel1Id,
c1.LocationLevel2Id,
c1.LocationLevel3Id,
c1.RoomNumber,
c1.RoomBarcode,
c1.RoomTypeId,
a1.VerificationStatusId,
a1.OldNumbers
}).Skip((page) * pageSize).Take(pageSize);
var projectAssets = query
.Select(a => new AssetContainerViewModel()
{
Id = a.Id,
AssetConditionId = a.AssetConditionId,
AssetOwnerTypeId = a.AssetOwnerTypeId,
Barcode = a.Barcode,
Username = a.Username,
SerialNumber = a.SerialNumber,
CaptureDate = a.CaptureDate,
CategoryLevel1 = new SimpleCategoryViewModel() { Id = a.CategoryLevel1Id, Text = a.CategoryLevel1Text },
CategoryLevel2 = a.CategoryLevel2Id == null ? null : new SimpleCategoryViewModel() { Id = (Guid)a.CategoryLevel2Id, Text = a.CategoryLevel2Text },
CategoryLevel3 = a.CategoryLevel3Id == null ? null : new SimpleCategoryViewModel() { Id = (Guid)a.CategoryLevel3Id, Text = a.CategoryLevel3Text },
CategoryLevel4 = a.CategoryLevel4Id == null ? null : new SimpleCategoryViewModel() { Id = (Guid)a.CategoryLevel4Id, Text = a.CategoryLevel4Text },
ContactId = a.ContactId,
ContactName = a.Name,
LocationLevel1Id = a.LocationLevel1Id,
LocationLevel2Id = a.LocationLevel2Id,
LocationLevel3Id = a.LocationLevel3Id,
RoomNumber = a.RoomNumber,
RoomBarcode = a.RoomBarcode,
RoomTypeId = a.RoomTypeId,
ContainerId = a.ContainerId,
//VerificationStatusId = a.VerificationStatusId,
VerificationStatus = (VerificationStatus)a.VerificationStatusId,
OldNumbers = a.OldNumbers,
VerificationStatusColor = ((VerificationStatus)a.VerificationStatusId).GetAttribute<EnumHelper.VerificationStatusAttribute>().Color
}).OrderBy(a => a.Barcode);
var result = projectAssets.ToDataSourceResult(request);
return result;
}
catch (Exception ex)
{
throw;
}
}