Linq to SQL get record with Max date value

If I was to write as follows...

var latest = _records.OrderByDescending(x => x.Date).First();

Is this ordering the complete set? Or is Linq clever enough to work out that you're only looking for the max value? Is there a more efficient way of writing the above?

--------------Solutions-------------

Yes, LINQ to SQL is smart enough to do the work on the database side. Your snippet will result in SQL like SELECT TOP (1) ... ORDER BY ....

(This is assuming that in your snippet _records represents a System.Data.Linq.Table<T> or something IQueryable, etc.)

As far as the optimal way to write the query, you might consider FirstOrDefault() if it's possible your table won't contain any records, in which case First() will throw an exception.

Category:linq to sql Time:2011-01-14 Views:0

Related post

  • LINQ to SQL selecting records and converting dates 2012-02-03

    I'm trying to select records from a table based on a date using Linq to SQL. Unfortunately the date is split across two tables - the Hours table has the day and the related JobTime table has the month and year in two columns. I have the following que

  • How can I stop an auto-generated Linq to SQL class from loading ALL data? 2010-03-12

    DUPLICATE of http://stackoverflow.com/questions/2433422/how-can-i-stop-an-auto-generated-linq-to-sql-class-from-loading-all-data post answers there! I have an ASP.NET MVC project, much like the NerdDinner tutorial example. (I'm using MVC 2, but follo

  • Duplicate LINQ to SQL entity / record? 2009-03-12

    What would be considered the best practice in duplicating [cloning] a LINQ to SQL entity resulting in a new record in the database? The context is that I wish to make a duplicate function for records in a grid of an admin. website and after trying a

  • Linq to SQL: Left Join to MAX aggregate 2010-02-24

    I am trying to write a Linq to SQL statement which displays all customer records and only the matching max(InvoiceId) of the invoice table; basically the newest invoice for the customer. The left join is required because a customer may not have any i

  • LINQ to SQL: Get records from two DB's that are on different servers 2010-04-22

    I need to fetch records from tables, that are in two diff. databases in two different SQL Servers. For Instance. Sales DB on server1 and Purchase DB on server2. Both Sales and Purchase DB's have some set of tables say table1 in Sales DB and table2 in

  • how to be sure in Linq to SQL that record has been added successfully or not? 2011-04-12

    I am just learning Linq to SQL and don't know much. Kindly guide and help me. I am adding a new record by using a SubmitChanges(); How I can be confirmed that record has been added? For example, in using stored procedures we use to send a flag back t

  • LINQ-to-SQL - Group By Week in Date object 2011-10-19

    I am a writing a query in LINQ-to-SQL, and in this query is a Date column. I want to group the results by the 'Week'. How would I do this in LINQ-to-SQL? Thanks --------------Solutions------------- Assuming Gregorian calendar, "first day" week rule a

  • LINQ to SQL separation and reusability of data structures 2011-11-04

    Here is simplified SQL of my tables, which is converted to LINQ to SQL model. CREATE TABLE Campaign ( Id int PRIMARY KEY, Name varchar NOT NULL ); CREATE TABLE Contract ( Id int PRIMARY KEY, CampaignId int NULL REFERENCES Campaign(Id) ); Now i have c

  • LINQ to SQL - Compile error when extending data context with partial class and methods 2009-03-04

    I am trying to use the Extensibility Method Definitions from my datacontext.designer.cs file to do some validation. So I created a new file and added this code: public partial class LawEnforcementDataContext : System.Data.Linq.DataContext { partial v

  • Return record with max date 2009-08-14

    Table – Employee.Status.XREF Client Employee Date 198 Jon Doe 3/1/2009 198 Jon Doe 1/1/2009 Table – Employee.Status.History Client Employee Date Status 198 Jon Doe 5/21/2009 T 198 Jon Doe 3/1/2009 A 198 Jon Doe 1/1/2009 P Only query records in both E

  • Linq to SQL EntitySet Records causing duplicate insertion 2009-12-18

    In a WPF application i'm using Linq to SQL in a multi tier application. (This is an archailogy photo filing application), so every excavation has its corresponding Pictures, thus a one-to-many relationship. This relationship is correctly created by S

  • LINQ to SQL does not update when data has changed in database 2010-04-19

    I have this problem where after a field (say Field3 in table MyTable) is updated on the database, MyTable.Field3 (in C#) is still returning the old value. I suspect there is some caching...? How do I force it to: Read the value from the database? OR

  • Confused on which one to to use - Linq to SQL, Datasets or Ado.Net Data entity model? 2010-07-30

    I'm a pretty descent old school ASPNET developer i.e I still use Visual studio 2005 & webforms to build websites. But with all the recent MVC hype I decided to get a copy of VS 2010 express and get aquainted with 'New school' ASPNET development.

  • Linq-to-SQL - how to make a data entity that is not attached to the database 2010-08-12

    Let's say I have a database that stores Fruit and FruitBaskets, and it's already populated with plenty of each. In my code I'm using Linq-to-Sql so that I can treat the rows of the database as instances of the OO classes Fruit and FruitBasket. Let's

  • Query SQL distinct field with max date, returning all fields from table 2011-01-13

    I'm having a hard time trying to get a query that sounds simple. I'm using SQL Server 2008. I have this table: ID Name Date Zone ---------------------------- 01 AAA 01/01/2010 North 02 BBB 02/02/2010 South 03 AAA 05/01/2010 West So, I need to filter

  • How to use Linq To Sql query to show the data in my case? 2011-06-08

    i have a trouble to write a query with Linq i explain better my case, i have a database with 2 tables as follow : it is the first table ; Hotel HotelID (Nvarchar(10) - PK) HotelName (Nvarchar (200)) and this one is the second table ; Period PeriodID

  • Finding correct record using MAX(date) 2011-07-22

    I've run into this problem that I've struggled with for some time now, reading a lot of post but not being able to solve it by my self. I've a table holding record of members status changes in a club. "Status_Members" MemberID Date StatusID 23 2011-0

  • Unit Testing, Linq to SQL and working around the Data Context 2009-09-08

    I have been looking at the following and it would appear that there are great benefits to be gained by using compiled queries... http://blogs.msdn.com/ricom/archive/2008/01/14/performance-quiz-13-linq-to-sql-compiled-query-cost-solution.aspx My probl

  • Linq to SQL, Update a lot of Data before One Insert 2011-05-28

    Before insert new value to table, I need change one field in all rows of that table. What the best way to do this? in c# code, ore use trigger? if C# can you show me the code? UPD *NEW VERSION of Question* Hello. Before insert new value to table, I n

Copyright (C) pcaskme.com, All Rights Reserved.

processed in 0.696 (s). 13 q(s)