2011-03-03 16 views
2

聲明我使用LINQ到SQL進行這樣的查詢:LINQ其中具有OR

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{...}; 

我怎樣寫這使OR和AND語句的工作?我已經嘗試了一個||和一個& &適當的地方,但它不工作,我堅持這個查詢。

基本上,它應該在一個月內返回一個日期列表,並且在MyModel中,我確實需要計數。例如,在一列中,我計算了某一天設定的約會次數,在另一列中,我計算了同一天出席的約會次數。日期1是指設置約會的日期,日期2是指約會的日期。因此,例如,2011年3月3日,我設置了4個約會(Date1是2011年3月11日),並且它們將在未來的不同日期(Date2)中設置。在同一日期(3月3日是Date2),我還參加了過去其他日期的其他幾項約會。

感謝您的任何建議。

回答

3
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{...}; 

嘗試刪除4個額外的「where」並將OR更改爲||。

+0

我試過了,它幾乎每個地方都是紅色的。我仍在嘗試。 – frenchie 2011-03-03 19:28:37

+0

好吧,我明白了你的意思是刪除額外的陳述。現在紅色下劃線是集體聲明的地方。我如何用AND語句進行分組? – frenchie 2011-03-03 19:34:23

+0

「分組依據」不正確。它只包含單詞「AND」。如果您想要通過多個事件進行分組,請使用「new {a = ..,b = ...}」並按匿名類型進行分組。 – vcsjones 2011-03-03 19:34:42