Linq LET和Dynamic Linq OrderBy

时间:2014-04-18 09:40:48

标签: c# .net linq nhibernate

我在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()
                              }
                     );

之后我想使用动态linq并像OrderBy一样使用OrderBy(&#34; EmployeeFullName DESC&#34;)而我无法做到这一点,因为我有异常。 我注意到,当我在任何我没有格式化或检查null等属性上执行OrderBy时,我不会遇到这个问题,只有在我尝试使用格式化属性进行orderby时才会出现这种情况。 如果我在OrderBy之前做ToList()然后我没有遇到这个问题,但我不想这样做,我需要使用IQueryable。

如何修改此查询以解决我的问题?

感谢您的帮助。

1 个答案:

答案 0 :(得分:0)

你试试这样:

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
                }