本文介绍了如何使用linq查询获取当前日期值?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述 我试图调用以下linq查询来显示最新的日期值,但目前我一直在遇到编译错误: API_09.Controllers.DateStampController.GetDate()':并非所有代码路径都返回值 public IEnumerable< database_BD> GetDate() { var data =( from c 在 db.database_BD 选择 new { c.UploadDate })。OrderByDescending(c = > c.UploadDate)。 Take( 1 )。ToList(); // 返回数据; } 如果在返回行中添加,则会显示另一个编译错误。 请帮忙。我的查询方法是不正确的? 提前谢谢。解决方案 我想你不是正确使用选择新也需要使用ToList()然后应用OrderBy 尝试以下: public IEnumerable< database_bd> GetDate() { var data =( from c 在 db.database_BD 选择 new { UploadDate = c.UploadDate })。ToList()。OrderByDescending(c = > c.UploadDate).Take( 1 ); 返回数据; } 请试试这个.... 公开IEnumerable的< database_BD> GetDate() { var data =(来自db.database_BD中的c 选择新的database_BD() { c.UploadDate })。OrderByDescending(c => c.UploadDate).Take(1).ToList(); 返回数据; } 请尝试完整代码 public IEnumerable< database_BD> GetDate() { IEnumerable< database_BD> data =(来自db.database_BD中的c 选择新的database_BD() { UploadDate = c.UploadDate })。OrderByDescending(c => c .UploadDate)。取(1).ToList(); 返回数据; } public class database_BD { public datetime UploadDate {获得; set ; } } Hi, I am trying to call the following linq query to display latest date value but currently i keep experiencing compiling error:API_09.Controllers.DateStampController.GetDate()': not all code paths return a valuepublic IEnumerable<database_BD> GetDate() { var data = (from c in db.database_BD select new { c.UploadDate }).OrderByDescending(c => c.UploadDate).Take(1).ToList(); //return data; }if add in the return line, it shows another compiling error. Please help. Is my approach to query is incorrect?Thanks in advance. 解决方案 I think you are not using the select new properly also need to use ToList() then apply OrderByTry below:public IEnumerable<database_bd> GetDate() { var data = (from c in db.database_BD select new { UploadDate=c.UploadDate }).ToList().OrderByDescending(c => c.UploadDate).Take(1); return data; }please try this one....public IEnumerable<database_BD> GetDate() { var data = (from c in db.database_BD select new database_BD() { c.UploadDate }).OrderByDescending(c => c.UploadDate).Take(1).ToList(); return data; }please try complete codepublic IEnumerable<database_BD> GetDate() { IEnumerable<database_BD> data = (from c in db.database_BD select new database_BD() { UploadDate = c.UploadDate }).OrderByDescending(c => c.UploadDate).Take(1).ToList(); return data; }public class database_BD { public datetime UploadDate{ get; set; } } 这篇关于如何使用linq查询获取当前日期值?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持! 09-22 14:09