There is a function and linq in it:

public Object Get(string title) { DbSet<Product> p; return p.Where(x => x.Title == title).Join(//Далее код) } 

Question: how can I ignore what is written in Where under a certain condition (for example, if title == null ) and continue further Join ???

 public async Task<Object> Get(string title, string group = null, string category = null) { return _productContext.gropsAndProducts .Join(_productContext.groups, gap => gap.GroupId, g => g.Id, (gap, g) => new { Id = gap.Id, ProductId = gap.ProductId, GroupTitle = g.Title, GroupDiscription = g.Discription, CategoryId = g.CategoryId }) .Where(x => x.GroupTitle == group) .Join(_productContext.categories, g => g.CategoryId, c => c.Id, (g, c) => new { Id = g.Id, ProductId = g.ProductId, GroupTitle = g.GroupTitle, GroupDiscription = g.GroupDiscription, CategoryId = g.CategoryId, CategoryTitle = c.Title, CategoryDiscription = c.Discription }) .Where(x => x.CategoryTitle == category) .Join(_productContext.product, c => c.ProductId, p => p.Id, (c,p) => new { Id = c.Id, ProductTitle = p.Title, Price = p.Price, GroupTitle = c.GroupTitle, GroupDiscription = c.GroupDiscription, CategoryTitle = c.Title, CategoryDiscription = c.Discription }) .Where(x => x.ProductTitle == title) } 

Everything works with the condition if non-empty values ​​are passed to the function.

  • The question is not quite clear. Where is applied to each element. Do you want to mark Where only for a particular item? Or if there is at least one element with title == null, then cancel Where for all? And anything, what at you in function p is not defined and will throw NullReferenceException? - AK
  • 3
    Where(x => x.Title == title || title == null) ? - tym32167 9:54 pm
  • @AK function for example. Look here: if the title comes null , then I would like everything to be without where - Vitaly Shebanits
  • 2
    if (title != null) p = p.where(...) ? - tym32167 9:57 pm
  • @ tym32167 Where(x => x.Title == title || title == null) doesn’t fit, if to title == null then I need to select absolutely all entries - Vitaly Shebanits

1 answer 1

If I understood correctly:

 IQueryable<Product> p = db.Products; // DbSet<Product> if (title != null) p = p.Where(x => x.Title == title); return p.Join(//Далее код); 
  • right now I will add the question, there are no nuances in it - Vitaly Shebanits