score:13
var first = result.first();
if the result set is empty, this will throw an exception; you can use firstordefault() which will return a null if the result set is empty.
score:0
you could also call result.take(1)
the difference between take(1)
and first()
is that first returns a single object, and take returns an ienumerable of the type.
score:0
if date is a reference type then you may consider the coalesce operator.
var lastdate = result.firstordefault() ?? new date();
score:0
if you're trying to find the latest date, you could use the "max" function (which i believe linq2sql will convert to a sql max operation):
var maxdate = db.table.max(a => a.date)
score:1
firstordefault() and as a bonus, you can use lastordefault() for... you guessed it...
[edit] -- oh, sudden rush there with the same answer :)
score:3
var lastdate = db.table.orderby(a => a.date).firstordefault();
score:8
call first()
.
for example:
lastdate =
(from a in db.table
orderby a.date descending
select a.date
).first();
if the table might be empty, call firstordefault()
, which will return datetime.minvalue
instead of throwing an exception.
Source: stackoverflow.com
Related Query
- Grabbing Just The Top Entry From A LINQ Query
- Linq query to group items and query from the top item in each group
- linq query to select top 3 from a group and get the average of those selected
- Moving code from the view into the LINQ query
- How to write linq query to match SQL like select top 100 * from tab?
- Take the first five elements and the last five elements from an array by one query using LINQ
- How to get the TSQL Query from LINQ DataContext.SubmitChanges()
- Enumerable.Empty<T>().AsQueryable(); This method supports the LINQ to Entities infrastructure and is not intended to be used directly from your code
- LINQ to Entities / LINQ to SQL: switching from server (queryable) to client (enumerable) in the middle of a query comprehension?
- How to understand the following C# linq code of implementing the algorithm to return all combinations of k elements from n
- Is there any way to create a LINQ query as a variable without having the data source (yet)?
- linq query to join two tables and get the count from one table values from the other
- How does linq actually execute the code to retrieve data from the data source?
- Injecting a variable into the Mono.CSharp.Evaluator (runtime compiling a LINQ query from string)
- How can I write the following code more elegantly using LINQ query syntax?
- How do I get results from a Linq query in the order of IDs that I provide?
- Determine the source DataContext for a Linq to Sql query
- Getting the first result from a LINQ query - why does ElementAt<T>(0) fails when First<T>() succeeds?
- C# Linq - how to select top 5 rows from a List<string> splitting on the first value
- Linq query for selecting an item from tree structure, but to look in the whole depth
- Using Linq to return the count of items from a query along with its resultset
- Update query in LINQ contains all columns in WHERE clause instead of just the primary key column
- Updating the original reference to an object that was returned from a linq query
- Is a full list returned first and then filtered when using linq to sql to filter data from a database or just the filtered list?
- creating Linq to sqlite dbml from DbLinq source code
- Why does this LINQ query assign a value of 1 to a NULL value from the database?
- How can I get the top three players and their high scores from a collection using linq and lambdas
- How do I check the values retrieved from this Linq query as .ToLower()?
- Send a new list to the result of the LINQ query from the list for C#
- Get the "latest" datetime from a large linq query that currently returns every record that has a datetime
More Query from same tag
- Interview Question: .Any() vs if (.Length > 0) for testing if a collection has elements
- EF LINQ spatial query times out using Contains() method in SQL Server
- Error using a subset of fields in Entity Framework
- How do I translate this unsupported LINQ query to its native Mongo equivalent?
- Entity Framework Map With Fake Foreign Key Property
- LINQ Query skips without exception. Why?
- How can I build a LINQ key selector delegate from a property name?
- DataTable sum duplicate rows
- Linq Queries with not operator
- Iterate through a ControlCollection from a CreateUserWizardStep
- Multiple Defered WHERE clause expressions in LINQ to SQL
- Linq Select All Items Matching Array
- LINQ How to select more than 1 property in a lambda expression?
- getting first level children?
- EntityFramework Extended how to update using values in a dictionary
- How to add to first array element?
- Unable to save with LINQ when trying to insert record with foreign keys?
- How to reuse code inside a Expression<Func< ... >>?
- Problem with sorting objects
- LINQ - Group By multiple keys not giving desired results
- What would you choose if you could use any .NET DAL technology?
- Filtering large collection of Unknown type
- Linq get single column data count
- Optionally loading/selecting a column in linq
- System.Collections.ArrayList.ToArray method is null
- How to avoid composing Where() operators in LINQ?
- Most efficient way to auto-hookup a foreign key in LINQ
- Is there a way to get all the items from a list that have a specific property
- Linq remove where if the value is null
- Linq to DataTable not producing Distinct values