Reputation: 28780
Is there anyway to change the following 2 linq expressions into 1?
var criticalCategories =
_commonDao.GetAllByExpression<CategoryItem>(
x => x.Category.Uid == gridAnswer.ActivityCategory.Uid && x.Critical);
if(criticalCategories.Any())
{
criticalWeight = criticalCategories.Min(x => x.Weight);
}
Upvotes: 0
Views: 42
Reputation: 437356
You can use Enumerable.DefaultIfEmpty
to make sure that Min
will produce a specific value if your source sequence contains no elements.
You could then write:
var criticalCategories = _commonDao.GetAllByExpression<CategoryItem>(...);
criticalWeight = criticalCategories
.Select(x => x.Weight)
.DefaultIfEmpty(42)
.Min();
The above is trivially chainable, but I did not actually chain it here because I 'm not quite sure how criticalCategories
is supposed to be used later on (if at all). Could you please clarify?
Upvotes: 2