Reputation: 2045
I use the code below to get records from two tables but I want to have the result in a List. I used the code below, but this error occured:
Cannot implicitly convert type
System.Collections.Generic.List<AnonymousType#1>
toSystem.Collections.Generic.List<QuickRoutes.DAL.Route>
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;
}
EDIT:
I want to match my query with this class:
namespace QuickRoutes.Entities
{
public enum RouteType
{
Route, Waypoints, Track
}
public sealed class Route
{
public int Id { get; set; }
public string Name { get; set; }
public DateTime Time { get; set; }
public List<TrackPoint> TrackPoints { get; set; }
public Guid UserId { get; set; }
public string GpxData { get; set; }
public RouteType RouteType { get; set; }
public Route(int id)
{
Id = id;
TrackPoints = new List<TrackPoint>();
}
......
but if I change my code to this one,It doesn't work too
List<Route> result = (from r in aspdb.RouteLinqs
where r.UserId == userId
join t in aspdb.TrackPointlinqs on r.RouteId equals t.RouteFK
select r).ToList();
to be honest I want to change this function that is written with SQL to LINQ,have any better idee?
namespace QuickRoutes.Entities
{
public enum RouteType
{
Route, Waypoints, Track
}
public sealed class Route
{
public int Id { get; set; }
public string Name { get; set; }
public DateTime Time { get; set; }
public List<TrackPoint> TrackPoints { get; set; }
public Guid UserId { get; set; }
public string GpxData { get; set; }
public RouteType RouteType { get; set; }
public Route(int id)
{
Id = id;
TrackPoints = new List<TrackPoint>();
}
......
Upvotes: 2
Views: 9345
Reputation: 3770
I have faced the same. The issue here is that you are creating an anonymous type that contains fields from both the Route and aspdb.TrackPoints.
Hence you cannot cast this to List.
This is a mapping problem. To resolve this you need to create a newcustom class that contains all the fields/properties that you are getting out from the query.
Let's say you create a custom class say RouteMapper then map using code like
select new RouterMapper
{
RouteMapper.TrackPointId = t.TrackPointId, RouteMapper.RouteFK=t.RouteFK, RouteMapper.SourceName = r.SourceName
}
This way now you return List< RouteMapper>.
Upvotes: 1
Reputation: 25201
Since your query selects properties from two different tables, it obviously no longer returns objects of type Route
, but a new type of object (an anonymous type).
If you want to return this new type, you need to define it explicitly, then return the data using this type.
public class MyClass
{
public MyClass(int trackPointId, DateTime trackTime, ...)
{
TrackPointId = trackPointId;
TrackTime = trackTime;
...
}
public int TrackPointId { get; set; }
public DateTime TrackTime { get; set }
...
}
List<MyClass> result = (from r in aspdb.Routes
where r.UserId == userId
join t in aspdb.TrackPoints on r.RouteId equals t.RouteFK
select new MyClass(t.TrackPointId,t.RouteFK,t.TrackTime,t.Longitude,t.Latitude,t.Elevation, r.SourceName);
Upvotes: 1
Reputation: 19646
It's because your projection (what you're selecting) isn't the same as a Route
(what you're trying to return);
If you wish to return a Route
Change your select to:
select r
If you wish to return your joined projection, you'l have to create a new type, and return that:
e.g:
public class RouteTrack
{
public TrackPointId ...
public RouteFK ...
public TrackTime ...
}
And return a List<RouteTrack>
from your method, and change your select to:
select new RouteTrack { TrackPointId = t.TrackPointId .... }
Upvotes: 2