Here is my current, working, query:
var lsFooterRow = from i in _context.Inventory
where i.ClaimId == claimID
&& i.Taxable == false
group i by new { i.ClaimId }
into grp
select new
{
SumOfReplValue = grp.Sum(i => i.Price),
SumOfACV = grp.Sum(i => i.ACV),
SumOfReplCost = grp.Sum(i => i.ReplacementCost)
};
What I’d like to add, to make it conditional, is something like this so that it adds a filter to the base query along with ClaimID and Taxable:
if (reportType == "R")
lsFooterRow = lsFooterRow.Where(i => i.ReplCost > 0);
This is not working because is does not recognize ReplCost, only SumOfReplValue, SumOfACV and SumOfReplCost.
Can someone please tell me, without doing the query in two steps, a way to add this condition? If there is no way to do it, a two step approach would be greatly appreciated 🙂
Thanks in Advance!
If I understand correctly, you should probably break out intial query into multiple pieces.
That way you are filtering on the replacement cost before it gets aggregated, which sounds like what you want to do.
You did express concerns about a two part query but that shouldn’t really pose a problem. The nice thing about this is it will not compose and execute the sql until you enumerate over the final version of the query. The entity framework engine is smart enough to simply add the second where as another condition in the final where statement in SQL. That means that your contitional where will neatly be part of the query rather than an afterthought. You can break up the query and add conditional things as much as you want.
The ability to break up the query into multiple pieces and conditionally compose the query is a tremendous benefit that LINQ has over SQL.