StudentHelper.cs
I've helper class called StudentHelper where It calls Repository Class to get all the student details. Below is the code to get the student details.
model = await _repo.FilterAsync<StudentRbCasesLink>(s =>
containsTest(options, list, s, countyId) && s.student.Deleted = false && s.student.studentId !=0,
s=> s.student,
s => s.studentRbCase,
s=> s.studentRbCase.CurrentCounty,
s=> s.studentRbCase.Abawdtype);
dto => _mapper.Map<IEnumerable<Objects.DTO.StudentRbCases>>(model);
containsTest is a method which does all the filtering based on the input parameters. This method gives back Boolean value based on the filter that is applied and that Boolean value is sent to repo class.
private bool containsTest(string options, Dictionary<string,string> list,
StudentRbCasesLink s,int countyId){
if(options.contains("FirstName")){
firstName = list["FirstName"].ToLower().ToString();
predicate = firstName != "&county" ?
(s.student.FirstName.ToLower().contains(firstName)).Tostring() :
"false";
}
if(options.contains("LastName")){
lastName = ............................
}
........................
return convert.ToBoolean(predicate);
}
Below is the actual FilterAsync Method that is in repo class. RepositoryClass.cs
public async Task<IQueryable<T>> FilterAsync<T>(Expression<Func<T,bool>> predicate, params Expression<Func<T,object>>[] includes) where T : class
{
return await Task.Run(()=> {
var query = _context.Set<T>().where(predicate).AsQueryable();
return includes.Aggregate(query, (current, includeProperty)
=> current.Include(includeProperty).asQueryable());
});
}
Let me describe the problem clearly. I'm doing a search by parameters functionality here. As soon as the studenthelper class is getting all parameters, it is hitting filterasync in studenthelper which in turn hits the actual method filterasync in Repository class. So, when I see the SQL profile for the converted SQL, It is showing the SQL Query with all the joins that are included in the includes in filterasync but coming to where condition it is applying only s.student !=0 condition in SQL which is making the query very slow (Not applying all the conditions/filters in where condition making it slow). It is not applying any conditions that are mentioned in containsTest method at the time SQL is generated but once the cursor hits the next Auto-mapper( is used to convert models to dtos) line, cursor is hitting containsTest Method and doing all the filters. Under the hoods, SQL is getting all the records and putting them in-memory and applying the filters when hitting Auto-mapper.
I've seen other posts where people suggested to put Expression> predicate instead of Func predicate. But, my code already have Expression. Can any one help me how to write the containsTest Method , so that where condition is applied while it is converted to SQL Query.Note that EF used is EntityFrameworkcore(1.1.2) Thanks for the help.
containsTest
method so it ignores it, instead it will drag the entire data set into memory and apply that function locally.Expression<…>
is good. But it should not contain calls to custom methods (and in general methods which are not supported/recognized by the query translator)containsTest
with a properExpression
that EF will understand and be able to parse into SQL