LINQ - How to values from Multiple tables using Joins
Asked By Sameer Khan on 20-Dec-11 06:48 AM
Hi,
I have 5 tables.The only common thing between these tables are the ID of the Person.
Now there is a final total field in each table. I want the final Total field of every table where ID=1.
Suppose there are these five table
ID Column
table1 1 final total
table2 1 final total
table3 1 final total
table4 1 final total
table5 1 final total
I know how to use the join on two table and get the value from a single table.But m clueless while getting records from Multiple tables.
How can i do this in the simplest possible way?
Please Guide
smr replied to Sameer Khan on 20-Dec-11 06:51 AM
hi
SQL JOIN joins together two tables on a matching table column, ultimately forming one single temporary table. The key word here is temporary. The tables themselves remain intact, and running a JOIN query does not in any way change the data or table structure. JOIN is another way to select specific data from two or more relational tables.
here is example
SELECT Customers.FirstName, Customers.LastName, SUM(Sales.SaleAmount) AS SalesPerCustomer
FROM Customers, Sales
WHERE Customers.CustomerID = Sales.CustomerID
GROUP BY Customers.FirstName, Customers.LastName
follow
http://www.techonthenet.com/sql/joins.php
http://www.sql-tutorial.net/SQL-JOIN.asp
Suchit shah replied to Sameer Khan on 20-Dec-11 06:54 AM
you can do it like below
select sum(T1.Total) as T1,sum(T2.Total) as T2,sum(T3.Total) as T3, sum(T4.Total) as T4,sum(T5.Total) as T5
from Table1 T1 INNER JOIN
Table2 T2 on T1.Id = T2.Id Inner Join Table3 T3 ON
T1.ID = T3.ID Inner join Table4 T4 ON
T1.ID = T4.Id INNER JOIN Table5 T5 ON
T1.ID = T5.ID
Web Star replied to Sameer Khan on 20-Dec-11 06:59 AM
you can simply join multiple table based on common field in linq query also
// Join on the ID properties.
var query = from c in customers
join o in orders on c.ID equals o.ID
select new { c.Name, o.Product };
// Display joined groups.
foreach (var group in query)
{
Console.WriteLine("{0} bought {1}", group.Name, group.Product);
}
You can join other table also as above in linq query
Sameer Khan replied to smr on 20-Dec-11 07:01 AM
Thanks for the Replies.
Sorry i forgot to mention ,but i want to do this in Linq and not in SQL. Im aware of SQl but i want to do the same in Linq.
PLease guide how can i do this in LINQ
Sameer Khan replied to Web Star on 20-Dec-11 07:07 AM
var query = from c in customers
join o in orders on c.ID equals o.ID
select new { c.Name, o.Product };
this is going to give me records from One Table
(ie customers )
But i want records from Multiple Table.
Sameer Khan replied to Web Star on 20-Dec-11 07:07 AM
var query = from c in customers
join o in orders on c.ID equals o.ID
select new { c.Name, o.Product };
this is going to give me records from One Table
(ie customers )
But i want records from Multiple Table.
Web Star replied to Sameer Khan on 20-Dec-11 11:38 AM
Try this way
var result = from a in dcMaster.Sl_tblPrjModules
join b in dcMaster.Sl_tblProjectProfiles on a.ProjectID equals b.ProjectID
select new { a, b };
return result.ToList();
It gives you record from both table a,b
mohd ghouse replied to Web Star on 04-Jan-12 08:31 AM
var qry = from c in drop.tblStudentDetails join s in drop.tblStudentA the data
ddresses on c.AddressId equals s.Id select new { c.Id, s.Address }; this is the query
but i want to retrive the data from anothe table that is tblstudent colm name is "name" how to retive
Web Star replied to mohd ghouse on 04-Jan-12 11:50 AM
It means you are trying to get data from three table than join your 3rd table also in that query
var result = from a in drop.tblStudentDetails
join b in drop.tblStudentA on a.StudentID equals b.StuID
join c in tblstudent on a.StudentId equals c.StuID
select new { a, b,c };
return result.ToList();
above query give you result from all three table which you need