2011-11-09 37 views
0

我正在實現一個簡單的搜索。現在,我可以使用字符串數組Split中的術語來搜索帖子,但我也想搜索日期。所以,我添加了一個datetime數組。我遇到的問題是我只想搜索日期時間的日期部分,並忽略時間。無論何時我搜索,我都會在d.Date處收到錯誤消息。陣列DateTime.Date與Linq查詢的兼容

IEnumerable<Post> posts = from p in post_repository.Posts 
          from s in split 
          from d in Date 
          where (p.Title.Contains(s) || p.ContactPhone.Contains(s) || p.Content.Contains(s) || p.Author.Contains(s) || p.ContactEmail.Contains(s)|| **p.EndDate.Date.Equals(d.Date)**) && p.Deleted == false && p.Publish == true 
          select p; 

錯誤消息:

The specified type member 'Date' is not supported in LINQ to Entities. Only initializers, entity members, and entity navigation properties are supported. 

Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code. 


Exception Details: System.NotSupportedException: The specified type member 'Date' is not supported in LINQ to Entities. Only initializers, entity members, and entity navigation properties are supported. 

我一直停留在這一段時間,任何人都可以找出我做錯了嗎? 所有的日期都被初始化了,所以沒有任何null和日期以外的其他東西都可以工作。如果我刪除d.Date或EndDate.Date,沒有錯誤,但我沒有得到所需的結果。 在此先感謝。

編輯1:

string[] split = query.Split(breakpoints); 
DateTime[] arrayOfDateTimes = Datify(split); 

宣言似乎很愚蠢。 Datify是我寫的一個函數,它返回一個正確工作的DateTime數組,因爲我一步一步地看着局部變量。

private readonly char[] breakpoints = {'.', ',', ' ', ':', '\t' }; 

查詢是搜索,它是一個字符串。

+3

您認爲Date是什麼? – Joe

+0

你能分裂你認爲你的查詢在做什麼嗎?給我們一個關於'split'和'Date'的聲明也會有幫助。就像那樣,那些第二個和第三個'from'表達式看起來是錯誤的。 –

+0

DateTime:10/10/2010 12:36:....日期將是10/10/2010日期時間的一部分。 我會盡量分開它,並寫出它的方式。 – user1034489

回答

1

使用類EntityFunction修剪時間部分。

using System.Data.Objects;

EntityFunctions.TruncateTime(EndDate)