使用LINQ减去表中的列

Bas*_*rdo 4 c# linq group-by sum linq-to-sql

我需要使用LINQ从数据库中的表中的另一列中减去一列

表试用

Userid  Money     Type

x       500       +

y       250       +

x       300       -

y       100       -

x       120       -
Run Code Online (Sandbox Code Playgroud)

我需要这样做;

500 - 300 - 120 对于x

250 - 100 因为你

我怎么做?

我尝试分组喜欢trial.useridtrial.money,trial.type.基本上,我认为我必须通过id分组用户,我需要添加+和减去 - 从添加的总和请帮助我.

Mar*_*ell 6

怎么样的:

var query = from row in db.Rows
            group row by row.UserId into tmp
            select new {
              UserId = tmp.Key,
              Money = tmp.Sum(x => x.Type == '+' ? x.Money : -x.Money)
            };
Run Code Online (Sandbox Code Playgroud)