Skip to content

Instantly share code, notes, and snippets.

@linuxbender
Created April 29, 2011 09:42
Show Gist options
  • Save linuxbender/948118 to your computer and use it in GitHub Desktop.
Save linuxbender/948118 to your computer and use it in GitHub Desktop.
Linq and SQL - group max date - show latest entry - example
// http://creativecommons.org/licenses/by-nc-sa/3.0/
var foo = from p in _db.tblFactories
group p by p.tblFactoryName into grp
let tblFactoryExportDate = grp.Max(p => p.tblFactoryExportDate)
let tblFactoryName = grp.Key
from p in grp
where p.tblFactoryName == tblFactoryName &&
p.tblFactoryExportDate == tblFactoryExportDate
select p;
-- http://creativecommons.org/licenses/by-nc-sa/3.0/
select f.[tblFactoryName],f.tblFactoryExportDate, f.tblFactoryID,f.tblFactoryUnitCount from tblfactory as f join
(
select [tblFactoryName], max(tblFactoryExportDate)as tblFactoryExportDate from tblFactory
group by [tblFactoryName]
) lastEntry on f.[tblFactoryName] = lastEntry.[tblFactoryName] AND f.tblFactoryExportDate = lastEntry.tblFactoryExportDate
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment