linq:检查特定表中是否存在同一天或同一周的条目

时间:2013-08-16 00:41:33

标签: c# .net linq-to-sql

所以我想要做的是获取数据库中的条目数,这些条目形成一个选定日期,从当天所在的那一周开始。数据库列和我的变量都是datetime字段。 我的约会方法是:

private int multiUse_timesInADay()
    {
           daydate date = currentDate.date;
        var submitCount = from campaignMessages in dcCRData.Mytable
                          where  campaignMessages.Created == date 
                          select campaignMessages;
        int count = submitCount.Count();

        return count;
    }

我对如何进行周搜索感到失望。 有什么想法吗?

1 个答案:

答案 0 :(得分:0)

//first get to the last monday i.e. start of the week

int delta = DayOfWeek.Monday - currentDate.date.DayOfWeek;
DateTime monday = currentDate.date.AddDays(delta);


var submitCount = from campaignMessages in dcCRData.Mytable
                  where  campaignMessages.Created >= monday &&  campaignMessages.Created <= monday.AddDays(7)    
                  select campaignMessages;