I developed a project with the DDD method to manage holidays. I have 2 value objects with the names of HolidayTitle and HolidayDate. Now I want to filter with Contains in the Query Repository but I can't.
My code in the repository is as follows:
public async Task<ListResponse<IList<GetHolidaysByDatesDto>>> GetHolidays(GetHolidaysQuery param)
{
var query = _repository.AsQueryable();
if (param.Date.HasValue)
query = query.Where(x => x.Date == param.Date);
if (!string.IsNullOrEmpty(param.Title))
query = query.Where(x => x.Title.Contains(param.Title));
var pagingData = await query.GetPagingData(param);
query = query.SetPaging(param);
var holidays = await query.ToListAsync();
var mappedResult = _mapper.Map<IList<GetHolidaysByDatesDto>>(holidays);
var finalResult = new ListResponse<IList<GetHolidaysByDatesDto>>()
{
PageCount = pagingData.PageCount,
PageNumber = pagingData.PageNumber,
RowCount = pagingData.RowCount,
Result = mappedResult
};
return finalResult;
}
And the codes of the HolidayTitle are as follows
public class HolidayTitle : BaseValueObject<HolidayTitle>
{
public static readonly int minLength = 5;
public static readonly int maxLength = 300;
public string Value { get; private set; }
private HolidayTitle(string value)
{
if (value is null)
throw new NullOrEmptyArgumentException(HolidayErrors.HolidayTitleIsNull);
value = value.Trim();
if (value == string.Empty)
throw new NullOrEmptyArgumentException(HolidayErrors.HolidayTitleIsEmpty);
if (value.Length > maxLength || value.Length < minLength)
throw new RangeLengthArgumentException(HolidayErrors.HolidayTitleLengthIsNotInRangeLength, minLength.ToString(), maxLength.ToString());
Value = value;
}
public bool Contains(string str)
{
return Value.Contains(str);
}
public override bool IsEqual(HolidayTitle otherObject)
{
return Value == otherObject.Value;
}
public override int ObjectGetHashCode()
{
return Value.GetHashCode();
}
public static implicit operator string(HolidayTitle value)
{
return value.Value;
}
public static HolidayTitle GetInstance(string value)
{
return new(value);
}
}
I get this error
System.InvalidOperationException: 'The LINQ expression 'DbSet<Holiday>()
.Where(h => !(h.IsDeleted))
.Where(h => h.Title.Contains(__param_Title_0))' could not be translated. Additional information: Translation of method 'Core.Domain.Holidays.ValueObjects.HolidayTitle.Contains' failed. If this method can be mapped to your custom function, see https://go.microsoft.com/fwlink/?linkid=2132413 for more information. Either rewrite the query in a form that can be translated, or switch to client evaluation explicitly by inserting a call to 'AsEnumerable', 'AsAsyncEnumerable', 'ToList', or 'ToListAsync'. See https://go.microsoft.com/fwlink/?linkid=2101038 for more information.'
CodePudding user response:
You cannot write
query = query.Where(x => x.Title.Contains(param.Title));
because EF does not know your HolidayTitle.Contains
method.
On the other hand, EF knows how to translate the method string.Contains
:
query = query.Where(x => x.Title.Value.Contains(param.Title));
CodePudding user response:
You have to use the below, as the query
is IQuerable
query.AsEnumerable().Where......