Table2:
id userid lat lon
1 1 14.000 15.000
2 2 14.3526 15.3698
by passing "venkat" as parameter then need to pull all matching records and his userid,name,lat,lon.
in above table1 "venkat" contains in both rows then need to pull 2 records.how to get userid,name,lat,lon for all matching rows..
for sigle record i am able to get.but there are multiple rows how to get please tell me....
If you have a relation between tables in db you can benefit from linqtosql associations:
var result= from p in yourDataContext.Table1
where p.name _ == "venket"
select new
{
Address= p.address_,
lat = p.Table2.lat ,
// add more you want...
};
venkateswara...
Member
16 Points
78 Posts
How to join the tables in linq to sql?
Apr 25, 2012 06:04 AM|LINK
Table1 :
userid name address
1 venkat srinagr
2 venkatesh sainagar
Table2:
id userid lat lon
1 1 14.000 15.000
2 2 14.3526 15.3698
by passing "venkat" as parameter then need to pull all matching records and his userid,name,lat,lon.
in above table1 "venkat" contains in both rows then need to pull 2 records.how to get userid,name,lat,lon for all matching rows..
for sigle record i am able to get.but there are multiple rows how to get please tell me....
Amin.Mirzapo...
Member
546 Points
98 Posts
Re: How to join the tables in linq to sql?
Apr 25, 2012 06:15 AM|LINK
Are you using LinqToSql Data context? and alos have you have a relation in you database for the tables?
venkateswara...
Member
16 Points
78 Posts
Re: How to join the tables in linq to sql?
Apr 25, 2012 06:16 AM|LINK
yes i am usng linq to sql datacontext.
rkchaudary
Contributor
2524 Points
545 Posts
Re: How to join the tables in linq to sql?
Apr 25, 2012 06:20 AM|LINK
from t1 in Table1, t2 in Table2
where t1.name like '%venkat%'
select t1.userid,t1.name,t2.lat,t2.lon
refer this : http://msdn.microsoft.com/en-us/vstudio/bb737929#selmany121-1
RkChaudary
blog
robwscott
Star
8079 Points
1491 Posts
Re: How to join the tables in linq to sql?
Apr 25, 2012 06:21 AM|LINK
var query = context.Table1.Include("Table2").Where(x => x.Name == "venkat") .Select(y => new { y.Table2.id, y.Table2.userid, y.Table2.lat, y.Table2.lon } ).ToList();Mark Answered if it helps - Good luck!
Cheers!
Design And Align
- Rob
Amin.Mirzapo...
Member
546 Points
98 Posts
Re: How to join the tables in linq to sql?
Apr 25, 2012 06:22 AM|LINK
If you have a relation between tables in db you can benefit from linqtosql associations:
var result= from p in yourDataContext.Table1 where p.name _ == "venket" select new { Address= p.address_, lat = p.Table2.lat , // add more you want... };venkateswara...
Member
16 Points
78 Posts
Re: How to join the tables in linq to sql?
Apr 25, 2012 06:56 AM|LINK
Hi Amin,
p.Table2.lat is not coming.
p.Table2.ElementAt(i).lat here how to pass i value.
var result= from p in cxt.Users
where p.Name == "venket"
select new
{
p.User_Id,p.Name,p.Location.ElementAt(i).Latitude,p.Location.ElementAt(i).Longitude
// add more you want...
};
venkateswara...
Member
16 Points
78 Posts
Re: How to join the tables in linq to sql?
Apr 25, 2012 07:01 AM|LINK
Hi robwscott,
Directly y.Table2.lat is not coming.
y.Table2.Element(i).latitude is coming.
Amin.Mirzapo...
Member
546 Points
98 Posts
Re: How to join the tables in linq to sql?
Apr 25, 2012 07:07 AM|LINK
Hi, You need to create a relationship between the two tables first:
http://msdn.microsoft.com/en-us/library/ms189049.aspx
www.youtube.com/watch?v=xL7h4VQhXFY
Then update your datacontext. (you can remove and add the tables again)
After then, you can use the solution I mentioned.
venkateswara...
Member
16 Points
78 Posts
Re: How to join the tables in linq to sql?
Apr 25, 2012 07:16 AM|LINK
i tried like as you told .
first created the relationship between two tables and after delete the datacontext and added the tables in solution.
but p.location.lat is not coming ,
p.location.element(i).latittude is coming