[英]Linq LET and Dynamic Linq OrderBy
I'm using linq in nhibernate and I have problem when I try to use OrderBy using Dynamic Linq: 我在nhibernate中使用linq,当我尝试使用Dynamic Linq使用OrderBy时遇到问题:
var bugAndFeatures = (from u in session.Query<BugAndFeature>()
let uEmployeeFullName = string.Format("{0} {1}", u.Employee.Surname, u.Employee.FirstName)
where u.IsDelete == false
select new BugAndFeatureView()
{
Id = u.Id,
Title = u.Title,
Description = u.Description,
EmployeeFullName = uEmployeeFullName,
EmployeeLogin = u.Employee.User.Login,
AssignedEmployeeId = u.AssignedEmployee == null ? 0 : u.AssignedEmployee.Id,
AssignedEmployeeFullName = string.Format("{0} {1}", u.AssignedEmployee.Surname, u.AssignedEmployee.FirstName),
CreateDate = u.CreateDate.Equals(null) ? string.Empty : u.CreateDate.ToString(),
LastUpdateDate = u.LastUpdateDate.Equals(null) ? string.Empty : u.LastUpdateDate.ToString()
}
);
After that I would like to use dynamic linq and use OrderBy like OrderBy("EmployeeFullName DESC") and I couldn't do that because I have exception. 之后,我想使用动态linq并像OrderBy(“ EmployeeFullName DESC”)一样使用OrderBy,但是我无法执行此操作,因为我有异常。 I noticed that when I'm doing OrderBy on any property which I don't format or check null etc. I don't have this problem it only occurs when I try to do orderby with formated properties.
我注意到当我在不格式化或不检查null的任何属性上执行OrderBy时,我没有此问题,仅当我尝试对格式化的属性进行orderby时才会发生。 If I do ToList() before OrderBy then I don't have this problem but I don't want to do that I need use IQueryable.
如果我在OrderBy之前执行ToList(),那么我没有这个问题,但是我不想这样做,我需要使用IQueryable。
How can I modify this query to solve my issue? 如何修改此查询以解决我的问题?
Thanks for help. 感谢帮助。
Have you try like this: 你有没有这样尝试过:
var Results = bugAndFeatures.ToList().OrderBy(p => p.EmployeeFullName);
IQueryable<Foo> query = ...;
switch (orderByParameter)
{
case "SomeValueParamter":
query = query.OrderBy(x => x.SomeValueParamter);
break;
case "SomeValueParamter":
query = query.OrderBy(x => x.SomeValueParamter);
break;
// etc
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.