开发者

Linq Sum Columns including joins

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.Cou开发者_开发百科pons] 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();


you already join the tables on the CouponID don't need it in the where:

var points = (
    from p in linq.Coupons
    join g in linq.games on p.couponID equals g.couponID
    where g.username == username
    select (int)p.win).Sum();


I solved the problem by using single to many relations instead of many to many in my database.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜