I'm using DynamicLinq on my Net Core Web API. The main purpose is to select data based on multiple columns filtered by OR
This is the Model
public class Car
{
public int Id { get; set; }
public string Name { get; set; }
public string Brand { get; set; }
}
The query looks like this. (I create generic method to handle all of the tables)
public static async Task<List<T>> Query<T>(this DbContext context, Dictionary<string, string> filter) where T : class
{
var query = context.Set<T>().AsQueryable();
string whereClause = "";
foreach(var d in filter)
{
if (whereClause != "")
whereClause += " || ";
whereClause += $"{d.Key}.Contains(\"{d.Value}\")";
}
return await query.Where(whereClause).ToListAsync();
}
This code generates no errors but the where clause is skipped which means it's executed like there's no Where and resulted all the data. The result query looks like SELECT Id, Name, Brand FROM Car where it should be like SELECT Id, Name, Brand FROM Car WHERE Name LIKE '%something%' OR Brand LIKE '%something%'
[EDIT]
Trying this online example works well when running customers.AsQueryable().Where("Name.Contains(\"David\") || Name.Contains(\"Gail\")")
I tested your generic method in both Dotnet 5 & Dotnet 6. everything was work correctly. I think your problem solve if you use the latest version of Dotnet & EfCore & Dynamic LINQ.