c# - Sum of Columns of Two Tables in LINQ -
i have 2 related tables in ms sql.
example... table – room roomid roomcost table b – roomitem roomitemid itemname itemcost roomid
i need in writing linq code add roomcost sum itemcost (if have same roomid) , post results in wpf textbox. far have tried
dataclassesdatacontext dc= new dataclassesdatacontext(); var roomamount = (from dbo in dc.room select dbo.roomcost).single(); var itemamount = (from itm in dc.roomitem select itm.itemcost).single(); decimal rmamount = convert.todecimal(roomamount); decimal itmamount = convert.todecimal(itemamount); decimal tamount=rmamount+itmamout; txttotalamount.text=tamount
i beginner in c#, kindly help. if haven't been clear enough, ask.
how using sum() (havent tested it):
assuming room.roomcost decimal , roomitem.itemcost decimal , myroomid id of room want sum up:
var roomamount = dc.room.single(r => r.roomid == myroomid).roomcost; var itemamount = dc.roomitem.where(i => i.roomid == myroomid).sum(r => r.itemcost); txttotalamount.text= roomamount + itemamount ;
Comments
Post a Comment