Setup
- Asp.Net Core 3.1
- EF Core 3.1
I am writing a LINQ statement to retrieve some entities from DB and I encountered a problem. This is the statement:
var Tag = "this is an input parameter";
location.SRID = 4326;
var posts = await _context.Posts
.Include(p => p.Location)
.Include(p => p.Event)
.Include(p => p.Repeatable)
.Include(p => p.Voucher)
.Include(p => p.Tags)
.ThenInclude(pt => pt.Tag)
.Where(p => p.ActiveFlag == 1 &&
p.Location.Location.IsWithinDistance(location, radius * 1000) &&
p.EventTime > today &&
p.Tags.All(pt => pt.Tag.TagName.Contains(Tag.ToLower()))) // always true
.AsNoTracking().ToListAsync();
The problem is that p.Tags.All(condition)
returns true for all, always. Looks like it's ignoring this condition. I even tried using TrueForAll()
instead, but that doesn't work at all with EF Core 3.1, it returns:
...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 either AsEnumerable()...
This is the shrunk Post model and its relationship with PostTags and Tags:
public class Post
{
public int Id { get; set; }
public virtual List<PostTags>? Tags { get; set; }
}
public class PostTags
{
public virtual Tag Tag { get; set; }
public int TagId { get; set; }
public virtual Post Post { get; set; }
public int PostId { get; set; }
}
public class Tag
{
public int Id { get; set; }
public string TagName { get; set; }
public virtual IEnumerable<PostTags>? Posts { get; set; }
}