简体   繁体   English

Linq查询日期的年份

[英]Linq Query for Year on a Date

Here is my code in the controller: 这是我在控制器中的代码:

}else if (reports == "Decommissioned")
            {
                if (reports != String.Empty)
                {
                    var date = (Convert.ToInt64(specific)) - 5;
                    desktop = desktop.Where(x => x.dt_date_delivered.Value.Year == date);
                    count = desktop.Where(x => x.dt_date_delivered.Value.Year == date).ToList().Count();
                }
            }

This query in inside a function in the controller. 该查询在控制器的函数内部。 I wonder why is this not working where other controller where I did not put this query in the function, it works. 我想知道为什么在我未在函数中放置此查询的其他控制器的情况下该方法不起作用。 When I try to run this code, the error is Cannot implicity ceonvert type 'System.Collections.Generic.IEnumerable<InSys_Models.Desktop_reports_vw>' to 'System.Collections.Generic.List<InSys.Models.Desktop_reports_vw>'. An explicit conversion exists(are you missing a cast?) 当我尝试运行此代码时,错误是Cannot implicity ceonvert type 'System.Collections.Generic.IEnumerable<InSys_Models.Desktop_reports_vw>' to 'System.Collections.Generic.List<InSys.Models.Desktop_reports_vw>'. An explicit conversion exists(are you missing a cast?) Cannot implicity ceonvert type 'System.Collections.Generic.IEnumerable<InSys_Models.Desktop_reports_vw>' to 'System.Collections.Generic.List<InSys.Models.Desktop_reports_vw>'. An explicit conversion exists(are you missing a cast?)

因为desktop.Where()函数返回IEnumerable,所以您应该调用ToList()将IEnumerable转换为List:

desktop = desktop.Where(x => x.dt_date_delivered.Value.Year == date).ToList();

您需要在Select的末尾调用ToList才能返回列表,而不是IEnumerable。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM