I am using C# application with Entity Framework version 7.0.4.
I have a building entity that one of its properties should be List, I use value converter to save this as comma separated string as the following:
public void Configure(EntityTypeBuilder<BuildingEntity> builder)
{
builder
.Building(x => x.DateGroup)
.HasConversion(new ValueConverter<List<DateTime>, string>(
v => string.Join(',', v.Select(i => i.ToString())), // Convert to string for persistence
v => v.Split(',', StringSplitOptions.None).Select(i => DateTime.Parse(i)).ToList())); // Convert to List<String> for use
}
So as to make the entity looks like this:
public class BuildingEntity
{
// some props
public List<DateTime>? DateGroup{ get; set; } = new ();
}
The issue is that when I try to make a query on that entity with filter on that column, I receive the following exception:
System.InvalidOperationException: The LINQ expression 'p => p.Date' could not be translated. 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'.
The query part that causing the issue is:
query = query
.Where(r => r.DateGroup != null &&
r.DateGroup.Select(p => p.Date).Contains(model.DateFilter.Value.Date)
);
DateFilter data type is Datetime?
I was expecting the query to filter the entities by the given date if it exists in one of the dategroup dates property.