Can anybody please guide me, how to add .ThenInclude(s) in Specification pattern ; i am using Entity framework Core.
I have below code for .Include, which is working.
public interface ISpecification<T>
{
Expression<Func<T, bool>> Criteria { get; }
List<Expression<Func<T, object>>> Includes { get; }
}
public abstract class BaseSpecification<T> : ISpecification<T>
{
protected BaseSpecification(Expression<Func<T, bool>> criteria)
{
Criteria = criteria;
}
public Expression<Func<T, bool>> Criteria { get; }
public List<Expression<Func<T, object>>> Includes { get; } = new List<Expression<Func<T, object>>>();
public List<string> IncludeStrings { get; } = new List<string>();
protected virtual void AddInclude(Expression<Func<T, object>> includeExpression)
{
Includes.Add(includeExpression);
}
}
}
Any help will be appreciated.
Found a solution to above problem. Below is the solution. Add in BaseSpecification class
protected virtual void AddInclude(string includeString)
{
IncludeStrings.Add(includeString);
}
After that in Specification filter use like below for then include.
AddInclude("Contacts.PrefixTitle");
This will include Contacts in Primary Class i.e. Account in my Case, then it includes Contacts then PrefixTitle