Created
April 29, 2011 09:42
-
-
Save linuxbender/948118 to your computer and use it in GitHub Desktop.
Linq and SQL - group max date - show latest entry - example
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
-- 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