2017-03-06 82 views
0

我想爲實體框架寫一個WHERE子句,其中Date包含輸入文本中的字符串,輸入文本可以是mm或mm/dd或mm/dd/yyyy,所以我我試圖寫我的WHERE子句,像這樣:ASP.NET Entity Framework其中日期包含

query.Where(p => (p.AccountingDate.Value.Month.ToString() + "/" + p.AccountingDate.Value.Day.ToString() + "/" + p.AccountingDate.Value.Year.ToString).Contains(gsStr)) 

,但我得到這個錯誤:

Operator '+' cannot be applied to operands of type 'string' and 'method group' 

什麼是做到這一點的最好方法是什麼?我真的很喜歡用Contains而不是==

請幫忙!

+1

爲什麼你想在某個日期使用'Contains'? – DavidG

+1

你在最後一個'ToString'後面缺少'()' – juharr

回答

2

Operator '+' cannot be applied to operands of type 'string' and 'method group'

你忘了調用.ToString方法:

"/" + p.AccountingDate.Value.Year.ToString 

應該是:

"/" + p.AccountingDate.Value.Year.ToString() 

前者解析爲方法本身(或 「法團」),後者做出決議到方法的結果(這是一個字符串)。

+0

謝謝,我知道它必須是一些愚蠢的東西 – user979331