I have the next models:
public class MaterialCatalog {
public int MaterialCatalogID { get; set; }
public int AccountID { get; set; }
public string MaterialName { get; set; }
public ICollection<MaterialCategory> Categories { get; set; }
}
public class MaterialCategory {
public int MaterialCatalogID { get; set; }
public int UserCategoryID { get; set; }
}
I need to create IQueryable generic external method for this expression:
var items = from l in ctx.WorkareaTemplates
where l.WorkareaTemplateCategories.Any(mc => mc.UserCategoryID == req.Category;
Here what I have tried. I get an error:
The double operator Equal is not defined for the types 'System.Collections.Generic.ICollection`1[MaterialCategory]' and 'System.Int32'.
How can I go to the subProperty to work with MaterialCategory?
public static IQueryable<TSource> FilterByUserCategoryId<TSource>(this IQueryable<TSource> source, object searchValue) {
if(typeof(TSource).GetProperties().Any(x => x.Name == "Categories")) {
var parameter = Expression.Parameter(typeof(TSource), "e");
var prop = Expression.Property(parameter, "Categories");
var value = Expression.Constant(searchValue);
var body = Expression.Equal(prop, value);
var lambda = Expression.Lambda<Func<TSource, bool>>(body, parameter);
var overload = typeof(Queryable).GetMethods().First(x => x.Name == "Any" && x.GetParameters().Length == 2);
var orderByGeneric = overload.MakeGenericMethod(typeof(TSource), prop.Type);
var result = orderByGeneric.Invoke(null, new object[] { source, lambda });
return (IQueryable<TSource>)result;
}
return source;
}