Linq Sum 列,包括连接
我有两张桌子。[桌面游戏]
列为 "PK_id"
"username"
和 "couponID"
[表格.优惠券]
列为 "PK_id"
"CouponID"
和 "Points"
两列[Table.Game]
中有两行包含用户“harry”,此人有两个不同的 couponID
。
在[Table.Coupons]
中,该用户“harry”的“CouponID”
为1和2。“Points”列有10和20
。问题是如何对这两个具有不同“CouponID”的不同点值求和。如果我只有一个“CouponId”
,这确实有效。但当用户有 2 个不同的 CouponID 时则不然。值为 0
var points = (from p in linq.Coupons
join g in linq.games on p.couponID equals g.couponID
where g.username == username && g.couponID == p.couponID
select (int)p.win).Sum();
I have two tables.[Table.Game]
Columns are "PK_id"
"username"
and "couponID"
[Table.Coupons]
Columns are "PK_id"
"CouponID"
and "Points"
The two columns "CouponID"
are associated with eachother. Let say i have two rows with the user "harry" in [Table.Game]
this person has two different couponID
.
In [Table.Coupons]
this user "harry" has "CouponID"
1 and 2. Column "Points"
have 10 and 20.
To the question how do u sum this two different point values that have different "CouponIDs"
. This does work if i have only one "CouponId"
. But not when the user has 2 different CouponIDs. Values is 0
var points = (from p in linq.Coupons
join g in linq.games on p.couponID equals g.couponID
where g.username == username && g.couponID == p.couponID
select (int)p.win).Sum();
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您已经在
CouponID
上加入表格,不需要在where
中使用它:you already join the tables on the
CouponID
don't need it in thewhere
:我通过在数据库中使用单对多关系而不是多对多关系解决了这个问题。
I solved the problem by using single to many relations instead of many to many in my database.