user1150380
user1150380

Reputation:

How to use Union in Linq Query

I have many tables.But there are Two columns common in each table. They are RegNo and Total.Now i want the values of all the total column for a particular RegNo. I can get that in different queries like this.

query=from k in db.MyTable1 where K.regNo=1 select k.Total
query2=from k in db.MyTable2 where K.regNo=1 select k.Total

This way but i want to do this and get the Summation of all Total's Column using one single Query Please guide.

Upvotes: 9

Views: 18470

Answers (1)

Sam M
Sam M

Reputation: 1087

You can do it this way.

   var itemCounts = (from k in db.MyTable1 where k.RegNO==1 select k.Total)
                     .Union(from k in db.MyTable2 where k.RegNO==1 select k.Total);
       TotalOfAll=itemCounts.Sum();

and using the sum method you can get the summation of all the Values in the query.

Upvotes: 13

Related Questions