Sign In| Join
Get Help:Ask a Question in our Forums|Report a Bug|More Help Resources
Last post Jan 06, 2013 02:43 AM by Decker Dong - MSFT
Member
548 Points
761 Posts
Jan 04, 2013 07:40 PM|LINK
If i have two tables where table 2 has a foreign key to table 1 i.e.
SELECT Customer.CustomerName, Product.Name, Product.Notes
FROM Customer
INNER JOIN Product ON Customer.CustomerID = Product.CustomerID
Then
1. How could/should i write the above query in Linq? 2. When i write a Linq query against one table i do something like:
dim query = from c in ctx.Customers Select new CustomerDTO With {.CustomerName = c.CustName} Return Query
How would i apply the same principle with two tables where one has a foreign key on the other table?
Thanks
Star
8822 Points
1502 Posts
Jan 04, 2013 07:50 PM|LINK
You can use this sentacx :
from t1 in db.Table1 join t2 in db.Table2 on t1.field equals t2.field select new { t1.field2, t2.field3}
for more informatin use this link :
http://blogs.msdn.com/b/tikiwan/archive/2010/06/18/linq-to-sql-inner-join-left-join-examples-advance-query.aspx
All-Star
118619 Points
18779 Posts
Jan 06, 2013 02:43 AM|LINK
EssCee 1. How could/should i write the above query in Linq?
Hi,
From some of your snippets, I think you can just use SQL converted to LINQ tool:
http://www.sqltolinq.com/downloads
EssCee
Member
548 Points
761 Posts
Query for two related tables
Jan 04, 2013 07:40 PM|LINK
If i have two tables where table 2 has a foreign key to table 1 i.e.
SELECT
Customer.CustomerName,
Product.Name,
Product.Notes
FROM
Customer
INNER JOIN Product ON Customer.CustomerID = Product.CustomerID
Then
1. How could/should i write the above query in Linq?
2. When i write a Linq query against one table i do something like:
dim query = from c in ctx.Customers
Select new CustomerDTO With {.CustomerName = c.CustName}
Return Query
How would i apply the same principle with two tables where one has a foreign key on the other table?
Thanks
Yousef_Jadal...
Star
8822 Points
1502 Posts
Re: Query for two related tables
Jan 04, 2013 07:50 PM|LINK
You can use this sentacx :
from t1 in db.Table1 join t2 in db.Table2 on t1.field equals t2.field select new { t1.field2, t2.field3}for more informatin use this link :
http://blogs.msdn.com/b/tikiwan/archive/2010/06/18/linq-to-sql-inner-join-left-join-examples-advance-query.aspx
Decker Dong ...
All-Star
118619 Points
18779 Posts
Re: Query for two related tables
Jan 06, 2013 02:43 AM|LINK
Hi,
From some of your snippets, I think you can just use SQL converted to LINQ tool:
http://www.sqltolinq.com/downloads