简体   繁体   中英

How to pass predicate to linq expression

Given a method signature like so...

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

How can I pass the predicate argument along to a linq expression?

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

}

Since predicate is a function that takes DataRow as an input -- use it like where predicate(student) .

var students = from student in _dataSet.Tables[0].AsEnumerable()
    ...
    where predicate == null || predicate(student)
    select student

我想你要:

where predicate == null || predicate(student)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM