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 Articles
- What can be used instead of Datatable in LINQ
- What is the event handler equivilent to the LINQ code used here
- What Sorting Algorithm Is Used By LINQ "OrderBy"?
- Enumerable.Empty<T>().AsQueryable(); This method supports the LINQ to Entities infrastructure and is not intended to be used directly from your code
- LINQ Source Code Available
- LINQ Datatable return 0 instead of null coalescing
- At what point is a LINQ data source determined?
- creating Linq to sqlite dbml from DbLinq source code
- Get what exists in one DataTable but not another using LINQ
- Can take be used in a query expression in c# linq instead of using .Take(x)?
- source code for LINQ 101 samples
- What SQL query or LINQ code would get the following data?
- What do i use with LINQ in order to READ database instead of SqlDataReader?
- LINQ to list<AJAX> to be used in DataTable
- Access to file is denied when LINQ is used instead of a foreach
- MVC 5 - Why is my linq code selecting an entire query instead of a single value?
- What is this linq code doing? Summarize something?
- Implicit operators, Linq and Lambda expressions can make code less readable. But what is more readable?
- c# Linq or code to extract groups from a single list of source data
- Linq Find Partial Text Match - Included code returns duplicate and everything except what it should
- What would be the Linq code for this grouping using VB.NET 2008?
- LINQ query on a DataTable
- What does LINQ return when the results are empty
- Convert string[] to int[] in one line of code using LINQ
- Code equivalent to the 'let' keyword in chained LINQ extension method calls
- Filling a DataSet or a DataTable from a LINQ query result set
- What is the difference between LINQ ToDictionary and ToLookup
- What guarantees are there on the run-time complexity (Big-O) of LINQ methods?
- What is LINQ and what does it do?
- Linq code to select one item
- Can we use overloaded Where operator in LINQ Query?
- How can I generate this query with Entity Framework to return the corresponding collections?
- How do I populate a Html.DropDownListFor<> with the data from an entity model?
- Get MethodInfo for ICollection<T>
- How to apply multiple joins as like in a SQL query in linq
- How to reinsert data from one table onto itself using LINQ in code migration?
- Filter values in linq orderby
- Is there a clever way to call a type dependent extension method dynamically?
- LINQ to map a datatable into a list<MyObject>
- Nested Async LINQ to get child objects into list
- linq collection of objects
- Tools for mapping business objects (DTO objects) from entities in asp.net mvc?
- Shorten this LINQ query via the help of an anonymous type?
- Convert/Parse many Objects using a member Method with less Code
- LinQ - Error when trying to order by child property
- need to do a subquery in Linq
- Using LINQ to extract Atrributes from XML
- LINQ GroupBy object or only anonymous types?
- Dynamic LINQ API - SQL Convert Function
- Too many outer joins in LINQtoSQL generated SQL