我正在使用動態Linq並具有where子句工作。現在我正在尋找添加orderby子句,但是有問題能夠設置動態表達式的類型。下面是工作,我有代碼:動態Linq - 在運行時設置order by表達式類型
class MyClass {
public string Owner;
public DateTime Inserted;
}
Expression<Func<MyClass, bool>> whereExpression = DynamicExpression.ParseLambda<MyClass, bool>("owner = \"joe\"");
Expression<Func<MyClass, DateTime>> orderExpression = DynamicExpression.ParseLambda<MyClass, DateTime>("inserted");
var result = from item in table.Where(whereExpression).OrderBy(orderExpression) select item;
result.ToList().ForEach(m => Console.WriteLine("inserted=" + m.Inserted + "."));
因爲我需要在排序依據表達想我能夠做到使用不同的屬性是使用類似下方未使用的代碼。
Expression<Func<MyClass, bool>> whereExpression = DynamicExpression.ParseLambda<MyClass, bool>("owner = \"joe\"");
Type orderType = typeof(DateTime);
Expression<Func<MyClass, orderType>> orderExpression = DynamicExpression.ParseLambda<MyClass, orderType>("inserted");
var result = from item in table.Where(whereExpression).OrderBy(orderExpression) select item;
result.ToList().ForEach(m => Console.WriteLine("inserted=" + m.Inserted + "."));
編譯器不滿意line delcaring orderExpression。有沒有辦法在運行時設置Func的類型?