Answer the question
In order to leave comments, you need to log in
How to fix my LINQ query?
Hello, I am making an online store and trying to implement the "similar products" function, that is, we go into a certain category of products, go into a view with detailed product descriptions and at the bottom we offer products similar to the category, the idea was to use LINQ queries with random output of records by categories from the database table, and output them to the view. See here is one of my requests:
public PartialViewResult Test2()
{
ListViewModel model = new ListViewModel
{
Furnitures = repository.Furnitures
.Where(x => x.Category.Equals("Кухни"))
.OrderBy(r => Guid.NewGuid()).Take(1)
};
return PartialView(model);
}
public PartialViewResult Test2(string category)
{
ListViewModel model = new ListViewModel
{
Furnitures = repository.Furnitures
.Where(x => category == null || x.Category == category)
.OrderBy(r => Guid.NewGuid()).Take(1),
CurrentCategory = category
};
return PartialView(model);
}
Answer the question
In order to leave comments, you need to log in
Do it just like in the first example.
If category is null or an empty string, then there will be no such category or there are goods that do not fall under the category.
You displayed all categories because the condition worked category == null
. In the debugger, make sure that the variable is set.
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question