0
\$\begingroup\$

I am getting products using given parameters (name of real entity and columns changed for illustration purposes),

 public IQueryable<Product> GetProductsByPara(string price, string weight, string dimensions, string trend)
 {
 var result = dbContext.Products.Where(a =>
 a.Price.ToLower().Equals(price.ToLower()) && 
 a.Weight.ToLower().Equals(weight.ToLower()) &&
 a.Dimensions.ToLower().Equals(dimensions.ToLower()) &&
 a.Trend.ToLower().Equals(trend.ToLower()));
 if (result.Any())
 {
 return result;
 }
 if (!string.IsNullOrWhiteSpace(price))
 {
 result = dbContext.Products.Where(a =>
 a.Price.ToLower().Equals(price.ToLower()) && 
 a.Weight.ToLower().Equals(weight.ToLower()) &&
 a.Trend.ToLower().Equals(trend.ToLower()));
 if (result.Any())
 {
 return result;
 }
 }
 if (!string.IsNullOrEmpty(dimensions))
 {
 result = dbContext.Products.Where(a =>
 a.Dimensions.ToLower().Equals(dimensions.ToLower()) &&
 a.Weight.ToLower().Equals(weight.ToLower()) &&
 a.Trend.ToLower().Equals(trend.ToLower()));
 if (result.Any())
 {
 return result;
 }
 }
 return result;
 }
asked Dec 17, 2019 at 11:52
\$\endgroup\$
2
  • \$\begingroup\$ Why are price and weight strings? That makes no sense. \$\endgroup\$ Commented Dec 18, 2019 at 16:57
  • \$\begingroup\$ @BCdotWEB As mentioned in question, names are changed for illustration purposes. thanks \$\endgroup\$ Commented Dec 19, 2019 at 7:22

2 Answers 2

1
\$\begingroup\$

Here is an approach to get rid of some of the redundancy:

First write an extension Method

static IQueryable<Product> FilterForParameters
 (this IQueryable<Product> query, string price, string weight, string dimensions, string trend)
{
 if (price != null)
 query = query.Where(p => p.Price.ToLower().Equals(price.ToLower()));
 if (weight != null)
 query = query.Where(p => p.Weight.ToLower().Equals(weight.ToLower()));
 if (dimensions != null)
 query = query.Where(p => p.Dimension.ToLower().Equals(dimensions.ToLower()));
 if (trend != null)
 query = query.Where(p => p.Trend.ToLower().Equals(trend.ToLower()));
 return query;
}

Then change your method

public IQueryable<Product> GetProductsByPara(string price, string weight, string dimensions, string trend)
{
 var products = dbContext.Products;
 var productsFilterdByAll = products.FilterForParameters(price, weight, dimensions, trend);
 if (productsFilterdByAll.Any())
 return productsFilterdByAll;
 var productsFilterdByAllButDimnensions = products.FilterForParameters(price, weight, null, trend);
 if (productsFilterdByAllButDimnensions.Any())
 return productsFilterdByAllButDimnensions;
 var productsFilterdByAllButPrice = products.FilterForParameters(null, weight, dimensions, trend);
 return productsFilterdByAllButPrice;
}

Depending on the context, it would also be a good idea to check your parameters for null at the beginning of the method.

answered Dec 18, 2019 at 15:09
\$\endgroup\$
0
\$\begingroup\$

use ternary operator as condition ? true : false;

public IQueryable<Product> GetProductsByPara(string price, string weight, string dimensions, string trend)
{
 return dbContext.Products.Where(a =>
 a.Price.Equals(!string.IsNullOrEmpty(dimensions) ? a.Price : price,StringComparison.OrdinalIgnoreCase) 
 && a.Weight.Equals(weight, StringComparison.OrdinalIgnoreCase) 
 && a.Dimensions.Equals(!string.IsNullOrWhiteSpace(price) ? a.Dimensions : dimensions, StringComparison.OrdinalIgnoreCase) 
 && a.Trend.Equals(trend, StringComparison.OrdinalIgnoreCase) 
 );
}

It is unnecessary to repeat:

if (result.Any())
{
 return result;
}

.

Also, using StringComparison.OrdinalIgnoreCase would be much more appropriate than ToLower()

answered Dec 17, 2019 at 13:26
\$\endgroup\$
1
  • 1
    \$\begingroup\$ OP is changing the content of result. So checking for result.Any() multiple times is actually a reasonable thing to do. \$\endgroup\$ Commented Dec 17, 2019 at 14:36

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.