C
C#2y ago
M B V R K

How to convert a Predicate to Expression

Hi friends, I have the method bellow My Question is is there any way or technique to Convert that Predicate to an Expression because the method should return an Expression ???
5 Replies
M B V R K
M B V R K2y ago
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 );

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 );

// 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;
}
// 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;
}
I hope someone here to suggest a solution and massive thanks in advance
dancepanda42
dancepanda422y ago
Expression<Func<Core.Entities.StudentAbsence, bool>> expression = (input) => predicate(input);
Expression<Func<Core.Entities.StudentAbsence, bool>> expression = (input) => predicate(input);
i think this should work
reflectronic
reflectronic2y ago
you can’t “convert” a predicate to an expression, except perhaps as an expression that invokes that predicate if you want the full expression tree you need to create it directly from the lambda
M B V R K
M B V R K2y ago
This one works perfectly Thanks you a lot mate
Accord
Accord2y ago
Ask the thread owner or member with permission to close this!
Want results from more Discord servers?
Add your server
More Posts