开发者

linq where statement with OR

I'm using linq-to-sql for a query like this:

public static List<MyModel> GetData(int TheUserID, DateTime TheDate)

using (MyDataContext TheDC = new MyDataContext())
{
   var TheOutput = from a in TheDC.MyTable
     where a.UserID == TheUserID
     (where a.Date1.Month == TheDate.Month && where a.Date1.Year == TheDate.Year)
     OR
     (where a.Date2.Month == TheDate.Month && where a.Date2.Year == TheDate.Year)
     group a by a.Date1.Date AND by a.Date2.Date into daygroups
 开发者_运维问答    select new MyModel{...};

How do I write this to make the OR and the AND statement work? I've tried putting a || and a && in place but it doesn't work and I'm stuck on this query.

Basically, it should return a list of days within a month and in the MyModel, I do counts. For instance, in a column I count the number of appointments set on a given day and in another column I count the number of appointments attended on the same day. Date1 refers to the date the appointments are set and Date2 refers to the dates the appointments are attended. So for example, on March 3rd 2011, I've set 4 appointments (Date1 is 3/11/2011 for these) and they're set for various dates in the future (Date2). During the same date (March 3rd is Date2 this time), I've also attended several other appointments that were set on other dates in the past.

Thanks for any suggestions.


using (MyDataContext TheDC = new MylDataContext())
{
   var TheOutput = from a in TheDC.MyTable
     where a.UserID == TheUserID &&   
     (a.Date1.Month == TheDate.Month && a.Date1.Year == TheDate.Year)
     ||
     ( a.Date2.Month == TheDate.Month && a.Date2.Year == TheDate.Year)
     group a by a.Date1.Date AND by a.Date2.Date into daygroups
     select new MyModel{...};

Try removing the 4 extra "where" and change the OR to ||.

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜