score:3

Accepted answer
var firstRoute = aspdb.RouteLinqs
    .Where(r => r.UserId == userId && r.RouteId == routeId)
    .FirstOrDefault();

if (firstRoute == null)
{
    return null;
}
else
{
    return new Route(routeId)
    {
        Name = first.SourceName,
        Time = first.CreationTime ?? new DateTime(),
        TrackPoints = GetTrackPointsForRoute(routeId)
    };
}

If this is LINQ to SQL you can simplify it further (this won't work with LINQ to Entity Framework though):

return aspdb.RouteLinqs
    .Where(r => r.UserId == userId && r.RouteId == routeId)
    .Select(r => new Route(routeId)
    {
        Name = r.SourceName,
        Time = r.CreationTime ?? new DateTime(),
        TrackPoints = GetTrackPointsForRoute(routeId)
    })
    .FirstOrDefault();

Note: You probably can replace GetTrackPointsForRoute with a join to the child table, meaning that the entire method can be done with a single call to the database, rather than one call to get the routes, and a second call to get the points. To do this you should learn about associations and joins in LINQ to SQL.


Related Articles