Reputation: 10129
I am having trouble figuring out how to do something like the following. This is purely pseudocode:
decimal totalActiveCost = (from i in _context.KeyActives
where i.Pk in (active1fk, active2fk, active3fk, active4fk, active5fk, keyActiveFk)
select sum(i.Cost)...`
Then summing the i.Cost. So basically, I need to return the i.Cost for each "Active" - so, for example, say active1fk is 1, active2fk is 2, and so on. I need to get the Cost for each of these and sum them up.
Upvotes: 0
Views: 87
Reputation: 3796
var ids = new List<int> {active1fk, active2fk, active3fk, active4fk, active5fk, keyActiveFk};
var sum = (from i in _context.KeyActives
where ids.Contains(i.Pk)
select i).Sum(a=> a.Cost);
Upvotes: 1
Reputation: 62488
Something like this will work:
List<Int> ids = new List<int>();
ids.Add(1);
ids.Add(2);
var result = _context.KeyActives.
Where(c => ids.Contains(c.id))
.Sum(c => c.Cost);
Upvotes: 1
Reputation: 223207
You can have your active foreign keys in a List<T>
like:
List<int> activeFks = new List<int> {1,2,3,4,5,};
var sum = (from i in _context.KeyActives
where activeFks.Contains(i.PK)
select i.Cost).Sum();
Or with a method syntax:
var sum = _context.KeyActives
.Where(r=> activeFks.Contains(r.PK))
.Sum(r=> r.Cost);
Upvotes: 5