开发者

Select most recent entry from database using LINQ to Entities

开发者 https://www.devze.com 2023-01-27 03:43 出处:网络
I need to select the most recent date from my database table and store it in a variable. I\'m not very familiar with linq to entities but I gave it a go myself however I only got so far.

I need to select the most recent date from my database table and store it in a variable. I'm not very familiar with linq to entities but I gave it a go myself however I only got so far.

This is how far I got:

IQueryable<DateTime> date = from ftp in ctn.FTPRuns
                            orderby ftp.LastRun descending
                            select ftp.Last开发者_如何学PythonRun;

Can anyone tell me how I can modify this query to just select the most recent entry. I have tried "select ftp.LastRun.First();" however this is not an option in intellisense for me.

any help would be appreciated.

thanks.


The result of what you have there (date) is an ordered IQueryable. You can just call date.First() to get the first one in that sequence.

Otherwise if you want to cut a step, wrap your query in brackets and call.First on it to just get the date.

DateTime date = (from ftp in ctn.FTPRuns  
                 orderby ftp.LastRun descending  
                 select ftp.LastRun).First(); 

You could otherwise use lambdas:

DateTime date = ctn.FTPRuns.OrderByDescending(f => f.LastRun).First().LastRun;


IQueryable<DateTime> dates = from ftp in ctn.FTPRuns
                             orderby ftp.LastRun descending
                             select ftp.LastRun;
DateTime date = dates.First();
0

精彩评论

暂无评论...
验证码 换一张
取 消