Krasimir
Krasimir

Reputation: 259

Linq subtraction sum(values) from two tables

I have two db.Table1 and db.Table2 with columns named IdUser,Value I think I should have some join but i miss the logic

it's just a logic it's not a code how can do something like :

var total = Sum(db.Table1(Sum(Value))-db.Table2(Sum(Value))
.Where(db.Table1.IdUser=db.Table2.IdUser)

Upvotes: 0

Views: 1666

Answers (1)

Novice
Novice

Reputation: 2487

Join the tables and group

    var total = from table1record in Table1
        join table2record in Table2 on table1Record.IdUser equals table2Record.IdUser
        group new { table1record,table2record } by table1record.IdUser into groupedRecords
        select groupedRecords.Sum(x=>x.Table1Value) -   groupedRecords.Sum(x=>x.Table2Value);

Upvotes: 1

Related Questions