C
C#2y 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;
}
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()
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()
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.
8 Replies
Becquerel
Becquerel2y ago
you will want to make a class that contains properties with your desired names that provides access to these predicates or return a named tuple, but that's yucky
M B V R K
M B V R K2y ago
hmmm, the problem is this method will works generically and the names of predicates will not be fixed
Becquerel
Becquerel2y ago
then I think you're out of luck if you want to use names at compile time, you have to know the names at compile time
M B V R K
M B V R K2y ago
hmmmm so this maybe impossible right ?
Becquerel
Becquerel2y ago
unless i'm missing something, and you don't wanna make a new class, then yeah i think so
M B V R K
M B V R K2y ago
Hmm this be done using Anonymous types ?
Becquerel
Becquerel2y ago
possibly i don't mess around with those too much
phaseshift
phaseshift2y ago
Make a class with normal properties (as already said), or return a dict and use string or enum as the key
Want results from more Discord servers?
Add your server
More Posts