aspnetdbDataContext aspdb = new aspnetdbDataContext();
result = (from r in aspdb.Routes
where r.UserId == userId
join t in aspdb.TrackPoints on r.RouteId equals t.RouteFK
select new {t.TrackPointId,t.RouteFK,t.TrackTime,t.Longitude,t.Latitude,t.Elevation, r.SourceName }).ToList();
You can't unless you add the same anonymous type. Notice how you're doing "select new { t.TrackPointId, ... }" -- that new call creates a new anonymous object. You'd have to do the same syntax to create a new anonymous object of the same shape.
I try to implement a Join with LINQ, I want to have fields from two tables and add this to a List becuase I have to return the resualt as a list this is my complete function und maybe you can understand what I want to do
public List<Route> GetAllForUser(Guid userId)
{
// var result = new List<Route>();
aspnetdbDataContext aspdb = new aspnetdbDataContext();
List<Route> result = (from r in aspdb.Routes
where r.UserId == userId
join t in aspdb.TrackPoints on r.RouteId equals t.RouteFK
select t.TrackPointId,t.RouteFK,t.TrackTime,t.Longitude,t.Latitude,t.Elevation, r.SourceName).ToList();
return result;
}
First create a model like this (modify based on your requirement):
public class NewRoute
{
[Key]
public int TrackPointId { get; set; }
public int RouteFK { get; set; }
public DateTime TrackTime { get; set; }
public string Longitude { get; set; }
public string Latitude { get; set; }
public string Elevation { get; set; }
public string SourceName { get; set; }
}
Secnd: Change your query:
List<NewRoute> GetAllForUser(Guid userId)
{
aspnetdbDataContext aspdb = new aspnetdbDataContext();
List<NewRoute> result = (from r in aspdb.Routes
where r.UserId == userId
join t in aspdb.TrackPoints on r.RouteId equals t.RouteFK
select new NewRoute()
{
TrackPointId = t.TrackPointId,
RouteFK = t.RouteFK,
TrackTime= t.TrackTime,
Longitude = t.Longitude,
Latitude = t.Latitude,
Elevation = t.Elevation,
SourceName = r.SourceName
}).ToList();
return result;
}
Babak_bsn
Member
53 Points
71 Posts
Linq query results to List collection
Jun 30, 2012 12:56 PM|LINK
Hi experts
I use below code
aspnetdbDataContext aspdb = new aspnetdbDataContext(); result = (from r in aspdb.Routes where r.UserId == userId join t in aspdb.TrackPoints on r.RouteId equals t.RouteFK select new {t.TrackPointId,t.RouteFK,t.TrackTime,t.Longitude,t.Latitude,t.Elevation, r.SourceName }).ToList();But I don't know how to add results to List?
Could any one tell me how !
BrockAllen
All-Star
27554 Points
4912 Posts
MVP
Re: Linq query results to List collection
Jun 30, 2012 01:11 PM|LINK
You can't unless you add the same anonymous type. Notice how you're doing "select new { t.TrackPointId, ... }" -- that new call creates a new anonymous object. You'd have to do the same syntax to create a new anonymous object of the same shape.
What are you trying to do?
DevelopMentor | http://www.develop.com
thinktecture | http://www.thinktecture.com/
Babak_bsn
Member
53 Points
71 Posts
Re: Linq query results to List collection
Jun 30, 2012 01:22 PM|LINK
I try to implement a Join with LINQ, I want to have fields from two tables and add this to a List becuase I have to return the resualt as a list this is my complete function und maybe you can understand what I want to do
public List<Route> GetAllForUser(Guid userId) { // var result = new List<Route>(); aspnetdbDataContext aspdb = new aspnetdbDataContext(); List<Route> result = (from r in aspdb.Routes where r.UserId == userId join t in aspdb.TrackPoints on r.RouteId equals t.RouteFK select t.TrackPointId,t.RouteFK,t.TrackTime,t.Longitude,t.Latitude,t.Elevation, r.SourceName).ToList(); return result; }jsiahaan
Contributor
2322 Points
596 Posts
Re: Linq query results to List collection
Jun 30, 2012 03:02 PM|LINK
Hi,
First create a model like this (modify based on your requirement):
public class NewRoute { [Key] public int TrackPointId { get; set; } public int RouteFK { get; set; } public DateTime TrackTime { get; set; } public string Longitude { get; set; } public string Latitude { get; set; } public string Elevation { get; set; } public string SourceName { get; set; } }Secnd: Change your query:
List<NewRoute> GetAllForUser(Guid userId) { aspnetdbDataContext aspdb = new aspnetdbDataContext(); List<NewRoute> result = (from r in aspdb.Routes where r.UserId == userId join t in aspdb.TrackPoints on r.RouteId equals t.RouteFK select new NewRoute() { TrackPointId = t.TrackPointId, RouteFK = t.RouteFK, TrackTime= t.TrackTime, Longitude = t.Longitude, Latitude = t.Latitude, Elevation = t.Elevation, SourceName = r.SourceName }).ToList(); return result; }Hope can help
Indonesian Humanitarian Foundation