1

複数の式を 1 つの式に結合し、それをパラメーターとして渡すにはどうすればよいですか?

// how to combine order by expression???
Expression<Func<Student, dynamic>> orderby = o => o.Marks desc then o.Name; /* ??? */

式を組み合わせることができるようになりましたが、desc と asc を示す方法は? OrderBy().ThenBy().ThenByDescending() のような拡張メソッドを使用できます。式を取り出すにはどうすればよいですか?

class Program
  {
    static void Main(string[] args)
    {
      // this is the normal extension method way
      IEnumerable<Student> students = Student.All.Where(o => o.Marks > 50).OrderByDescending(o => o.Marks).ThenBy(o => o.Name).Skip(10).Take(10);

      // construct filter, and I can combine this kind of expression
      Expression<Func<Student, bool>> filter = o => o.Marks > 50;

      // but how to combine order by expression???
      Expression<Func<Student, dynamic>> orderby = o => o.Marks desc then o.Name; /* ??? */

      IEnumerable<Student> howtogetstudents = GetStudents(filter, orderby, 2, 10);
    }

    static IEnumerable<Student> GetStudents(Expression<Func<Student, bool>> filter,
      Expression<Func<Student, dynamic>> orderby, int pageNumber, int pageSize)
    {
      IQueryable<Student> students = Student.All;

      return students.Where(filter).OrderBy(orderby).Skip((pageNumber - 1) * pageSize).Take(pageSize);
    }
  }

  class Student
  {
    Student() { }

    public int Id { get; set; }
    public string Name { get; set; }
    public DateTime Birthday { get; set; }
    public decimal Marks { get; set; }

    public static IQueryable<Student> All { get { return null; } }
  }
4

0 に答える 0