C#C
C#4y ago
M B V R K

How to return a named predicates

Hi friends,
I want to build a Method that returns a set of Predicates

Example:
    private Predicate<Core.Entities.StudentAbsence>[] GetPredicates( SearchStudentAbsencesQuery query )
    {
        var whereFullNameContainsValue = new Predicate<Core.Entities.StudentAbsence>( x => ( x.StudentEnrollment.Student.FirstName + x.StudentEnrollment.Student.FamilyName ).Contains( query.Value ) );
        var whereSchoolSubjectTitleContainsValue = new Predicate<Core.Entities.StudentAbsence>( x => x.SchoolSubject.FullTitle.Contains( query.Value ) );
// Other predicates

        var predicates = new[]
                         {
                             whereFullNameContainsValue ,
                             whereSchoolSubjectTitleContainsValue ,
                             // Others
                         };
        
        return predicates;
    }


The Issue:
How do I can return those predicates but everyone with a name like properties, let me explain more

depending on the example I provided the use of that result will be like this:
var predicates = GetPredicates(Query);

var result = await db.<StudentAbsence>().Where( predicates[0] || predicates[1] //... ).ToListAsync()

as you will notice that to get every predicate I should use its index, instead of that I want when I got the predicates use their names like this following
var predicates = GetPredicates(Query);

var result = await db.<StudentAbsence>().Where( predicates.whereFullNameContainsValue  || predicates.whereSchoolSubjectTitleContainsValue //... ).ToListAsync()


I hope the explanation of the issue is clear, please is this even possible, if yes how do I can achieve this ? and massive thanks.
Was this page helpful?