如何比较Linq for Date Fields的年份?

时间:2015-12-30 05:41:01

标签: c# asp.net linq asp.net-mvc-4

我使用以下代码来比较两个日期的年份:

public string GetResultWbyYears()
    {
        using (var context = new DbEntity())
        {
            int yeat = DateTime.Now.Year;
            var holidays = (from rl in context.HolidayMasters
                            where rl.Isactive == true && rl.Isdelete == false //&& rl.HolidayDate.Year == yeat
                            orderby rl.HolidayDate ascending
                            select new HolidayEntity
                            {
                                HolidayDesc = rl.HolidayDesc,
                                HolidayDate = rl.HolidayDate,
                                HolidayId = rl.HolidayId

                            }).ToList().AsEnumerable().Select(x => new HolidayEntity()
            {
                HolidayDesc = x.HolidayDesc,
                HolidayDate = x.HolidayDate,
                Isactive = x.Isactive,
                Isdelete = x.Isdelete,
                HolidayId = x.HolidayId,
                Zones = GetZonesWithSelectedForHolidays(x.HolidayId)
            }).ToList();

            holidays = holidays.Where(n => n.HolidayDate.Year == yeat).ToList();
        }

不知道为什么以上不起作用..请帮助我如何才能获得年度结果。

我在谷歌搜索过并发现一些文章做了我上面做过的同样的事情,但这对我不起作用。这就是我发布问题的原因。

1 个答案:

答案 0 :(得分:1)

您选择IEnumerable<HolidayEntity>,然后将其转换为List,丢失IsactiveIsdelete值,因为它们将是默认值,然后再次选择IEnumerable<HolidayEntity>。<登记/> 实际上没有必要这样做。

您的LINQ看起来更漂亮:

public string GetResultWbyYears()
{
    int year = DateTime.Now.Year;
    using (var context = new DbEntity())
    {
        var result = context.HolidayMasters
            .Where(hm => hm.Isactive && !hm.Isdelete && hm.HolidayDate.Year == year)
            .OrderBy(hm => hm.HolidayDate)
            .ToList();

        foreach (var item in result)
            item.Zones = GetZonesWithSelectedForHolidays(item.HolidayId); 
    }
}

或以其他方式:

public string GetResultWbyYears()
{
    int year = DateTime.Now.Year;
    using (var context = new DbEntity())
    {
        var result = (from hm in dbSet
            where hm.Isactive && !hm.Isdelete && hm.HolidayDate.Year == 2015
            orderby hm.HolidayDate ascending
            select hm).ToList();

        foreach (var item in result)
            item.Zones = GetZonesWithSelectedForHolidays(item.HolidayId); 
    }
}    

我认为您的context.HolidayMasters类型为DbSet<HolidayEntity,因为它具有相同名称的所有属性。否则,请使用一个Select

public string GetResultWbyYears()
{
    int year = DateTime.Now.Year;
    using (var context = new DbEntity())
    {
        var result = (from hm in dbSet
            where hm.Isactive && !hm.Isdelete && hm.HolidayDate.Year == 2015
            orderby hm.HolidayDate ascending
            select new HolidayEntity {
                HolidayDesc = x.HolidayDesc,
                HolidayDate = x.HolidayDate,
                Isactive = x.Isactive,
                Isdelete = x.Isdelete,
                HolidayId = x.HolidayId,
                Zones = GetZonesWithSelectedForHolidays(x.HolidayId)
            }).ToList();
    }
}