C
Join ServerC#
help
How to convert a Predicate to Expression
MMBARK8/28/2022
Hi friends, I have the method bellow
My Question is is there any way or technique to
My Question is is there any way or technique to
Convert
that Predicate
to an Expression
because the method should return an Expression
???MMBARK8/28/2022
Method:
public Expression<Func<Core.Entities.StudentAbsence , bool>> GetPredicate( 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 ) );
var whereAbsenceDateContainsValue = new Predicate<Core.Entities.StudentAbsence>( x => x.AbsenceDate.ToFrenchDateString().Contains( query.Value ) );
var whereAbsenceTimeContainsValue = new Predicate<Core.Entities.StudentAbsence>( x => x.AbsenceTime.ToTimeString().Contains( query.Value ) );
var whereCombinedDataAndTimeContainsValue = new Predicate<Core.Entities.StudentAbsence>( x => ( x.AbsenceDate.ToFrenchDateString() + " " + x.AbsenceTime.ToTimeString() ).Contains( query.Value ) );
var whereStudentIdEqualsValue = new Predicate<Core.Entities.StudentAbsence>( x => x.StudentEnrollment.StudentId.ToString().Contains( query.Value ) );
var whereBranchEqualsValue = new Predicate<Core.Entities.StudentAbsence>( x => x.StudentEnrollment.Group.BranchId == query.Value );
var whereGroupEqualsValue = new Predicate<Core.Entities.StudentAbsence>( x => x.StudentEnrollment.GroupId.ToString() == query.Value );
MMBARK8/28/2022
// Combine the predicates into a single
var predicate = new Predicate<Core.Entities.StudentAbsence>( x =>
whereFullNameContainsValue( x ) ||
whereSchoolSubjectTitleContainsValue( x ) ||
whereAbsenceDateContainsValue( x ) ||
whereAbsenceTimeContainsValue( x ) ||
whereCombinedDataAndTimeContainsValue( x ) ||
whereStudentIdEqualsValue( x ) ||
whereBranchEqualsValue( x ) ||
whereGroupEqualsValue( x ) ||
( whereGroupEqualsValue( x ) && whereAbsenceDateContainsValue( x ) ) );
// Convert the predicate to an expression
var expression = // Idon't kknow how to do it
return expression;
}
MMBARK8/28/2022
I hope someone here to suggest a solution
MMBARK8/28/2022
and massive thanks in advance
Ddancepanda428/28/2022
Expression<Func<Core.Entities.StudentAbsence, bool>> expression = (input) => predicate(input);
Ddancepanda428/28/2022
i think this should work
Rreflectronic8/28/2022
you can’t “convert” a predicate to an expression, except perhaps as an expression that invokes that predicate
Rreflectronic8/28/2022
if you want the full expression tree you need to create it directly from the lambda
MMBARK8/28/2022
This one works perfectly
MMBARK8/28/2022
Thanks you a lot mate
AAccord8/28/2022
Ask the thread owner or member with permission to close this!