2

给定这样的方法签名......

IEnumerable<Student> get(Func<DataRow, bool> predicate);

如何将谓词参数传递给 linq 表达式?

public override IEnumerable<Student> get(Func<System.Data.DataRow, bool> predicate = null) {
    var students = from student in _dataSet.Tables[0].AsEnumerable()
              join attendance in _dataSet.Tables[1].AsEnumerable() 
              on student.Field<int>("id") equals attendance.Field<int>("idStudent")
              join grade in _dataSet.Tables[2].AsEnumerable()
              on student.Field<int>("id") equals grade.Field<int>("idStudent")
              join assignment in _dataSet.Tables[3].AsEnumerable()
              on student.Field<int>("id") equals assignment.Field<int>("idStudent")
              // where predicate??
              // select blahblahblah
    // return statement

}
4

2 回答 2

3

由于是一个将其作为输入predicate的函数- 使用它就像.DataRowwhere predicate(student)

var students = from student in _dataSet.Tables[0].AsEnumerable()
    ...
    where predicate == null || predicate(student)
    select student
于 2013-07-18T22:48:32.650 回答
2

我想你想要:

where predicate == null || predicate(student)
于 2013-07-18T22:51:35.363 回答