我有BinaryTree<Student>
deserseriizedstudents和linq查询:
var students = DeserializedStudents.OrderBy(testResult => testResult.Test_Result).
Select(cust => new { name = cust.Name, result = cust.Test_Result });
有人能告诉我如何建立一个表达式,等于这个查询?
对于复杂的表达式,您可以欺骗并查看编译器的操作:
Expression<Func<IEnumerable<Student>, IEnumerable<Student>>> expression =
query => query
.OrderBy(testResult => testResult.Test_Result)
.Select(cust => new { name = cust.Name, result = cust.Test_Result });
看看调试器中的expression
。
顺便说一句,LINQPad在这方面很有帮助。
您可以使用IQueryable
接口实现来自动构建基于LinQ
的表达式:
var query = DeserializedStudents.AsQueryable()
.OrderBy(testResult => testResult.Test_Result)
.Select(cust => new { name = cust.Name, result = cust.Test_Result });
var expression = query.Expression;
像这样?:
var students = from cust in DeserializedStudents
orderby cust.Test_Result
select new { name = cust.name, result = cust.Test_Result };