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.
Source: stackoverflow.com
Related Query
- What can be used instead of Datatable in LINQ
- Can take be used in a query expression in c# linq instead of using .Take(x)?
- What is the event handler equivilent to the LINQ code used here
- Implicit operators, Linq and Lambda expressions can make code less readable. But what is more readable?
- What can I do to resolve a "Row not found or changed" Exception in LINQ to SQL on a SQL Server Compact Edition Database?
- Can LINQ be used in PowerShell?
- What Sorting Algorithm Is Used By LINQ "OrderBy"?
- Can LINQ be used to find gaps in a sorted list?
- How I can filter a dataTable with Linq to datatable?
- What construction can I use instead of Contains?
- Enumerable.Empty<T>().AsQueryable(); This method supports the LINQ to Entities infrastructure and is not intended to be used directly from your code
- Where can I view LINQ source code?
- What can I use instead of an Include?
- LINQ Source Code Available
- How can I switch that code to use LINQ
- What is the Maximum Number of Records i can insert through InserAllOnSubmit() In Linq to Sql
- How can I combine this code into one or two LINQ queries?
- How can I dynamically store expressions used for Linq Orderby?
- Can LINQ be used to pull keywords out of a string?
- Can LINQ be used in Dynamics CRM to get all Accounts not in a Collection?
- How can I write the following code more elegantly using LINQ query syntax?
- How can I further simplify this piece of LINQ code
- What can map database tables like LINQ to SQL did?
- How can I code an outer join using LINQ and EF6?
- LINQ Datatable return 0 instead of null coalescing
- Can Linq be used to find if value matches part of a larger string?
- Can LINQ expression classes implement the observer pattern instead of deferred execution?
- Can LINQ be used to search for multiple Regex expressions in a string?
- What Linq query can I use to return a count of all products by Category?
- Can these two LINQ queries be used interchangeably?
More Query from same tag
- Paging in Gridview with Linq
- Translating VB.NET LINQ Query with Sum to C# LINQ Query
- LINQ in typescript or any alternative
- How to return IGrouping from Linq query when grouping
- Access to modified closure... but why?
- Prune tree branches where ending nodes meet some criteria
- How are attached properties useful in LINQ?
- How to the get an object based on 2 criteria without doing extensive searching?
- Display list of distinct book names from LINQ query
- Entity Framework calling database SQL string functions using Linq?
- Linq Where is not recognizing operator overloads with custom object
- Query NHibernate Properties with LINQ
- LINQ OrderBy on inner object's property
- Linq Expression tree compiling non-trivial object constants and somehow referring to them
- Linq 'where' clause returning CS1929
- How do I create a linq query that gets everything but a specific value
- Can a method chain be called LINQ?
- Explanation of this LINQ code which generates permutations
- Linq Converting String to List
- Can you set a property in a linq query before selecting the item?
- Applying IEnumerable<'T>.OrderBy on a IQueryable<'T>
- Performance issues with LINQ query when including OR clause
- get partition ((N-1)-dimensional instance of the N-dimensional array) of a multidimensional array with LINQ
- How to read 3 lines of a text file c#
- Search through a 3d list using LINQ
- POST request throwing null exception
- LINQ gives different results against XML and Db
- Filter Parent Collection Using Criteria in Child Collection via a Linq Query
- c# Web Service Sorting Result
- Linq - Remove object in nested collections