I have Products table which looks like this:
I have some data in this table and I want to filter it depending on this columns (Filtering with columns type of string isn't problem). For this I have the code (in ASP.NET Core Web API Project) shown below. I know it's not a good approach. I have another table which has more foreign keys, so it will be more difficult to do this kind of work on it.
In SearchProduct action method I am getting the product object, which properties have specific values. For example, If Name="Box"
, then I want to get products from the product list, which Name is "Box" too.
The consumer of this service is WPF application, where I have the window, where the controls (textboxes and comboboxes{ for productType and ProductUnit}) are placed and the user will be able to search data with any criteria. For example, if he wants to search only with Name, he will write something only in Name textbox. If he wants to search product which name is "Box" and also the productType is "A", then he will write "Box" in name textbox and will choose A type in comboBox.
P.S When product is added in this table, if user doesn't write anything in the Name textbox for example, the value of Name will be "" and not null.
[HttpPost("SearchProduct")]
public IEnumerable<Product> SearchProduct([FromBody]Product product)
{
IEnumerable<Product> data;
if(product.ProductTypeID != null && product.ProductUnitID != null)
{
data = _context.Products.Where(x =>
x.Name.Contains(product.Name) &&
x.Code.Contains(product.Code) &&
x.Barcode.Contains(product.Barcode) &&
x.InnerCode.Contains(product.InnerCode) &&
(x.ProductTypeID == product.ProductTypeID &&
x.ProductUnitID == product.ProductUnitID))
.Include(x => x.ProductType)
.Include(x => x.ProductUnit);
}
else if(product.ProductTypeID == null && product.ProductUnitID == null)
{
data = _context.Products.Where(x =>
x.Name.Contains(product.Name) &&
x.Code.Contains(product.Code) &&
x.Barcode.Contains(product.Barcode) &&
x.InnerCode.Contains(product.InnerCode) ||
(x.ProductTypeID == product.ProductTypeID &&
x.ProductUnitID == product.ProductUnitID))
.Include(x => x.ProductType)
.Include(x => x.ProductUnit);
}
else if(product.ProductTypeID != null)
{
data = _context.Products.Where(x =>
x.Name.Contains(product.Name) &&
x.Code.Contains(product.Code) &&
x.Barcode.Contains(product.Barcode) &&
x.InnerCode.Contains(product.InnerCode) &&
x.ProductTypeID == product.ProductTypeID)
.Include(x => x.ProductType)
.Include(x => x.ProductUnit);
}
else
{
data = _context.Products.Where(x =>
x.Name.Contains(product.Name) &&
x.Code.Contains(product.Code) &&
x.Barcode.Contains(product.Barcode) &&
x.InnerCode.Contains(product.InnerCode) &&
x.ProductUnitID == product.ProductUnitID)
.Include(x => x.ProductType)
.Include(x => x.ProductUnit);
}
return data;
}
.ToList()
anywhere. The context should already be disposed when the query is executed... I think this would throw an excepiton. \$\endgroup\$