2017-03-06 82 views
0

我正在使用以下代碼從Lucene索引中獲取結果。如何在IQueryable中爲Lucene使用DbFunctions索引搜索

using (var context = ContentSearchManager.GetIndex("my_profile_index").CreateSearchContext()) 
    { 
     IQueryable<ProfileSearchItem> query = from profile in context.GetQueryable<ProfileSearchItem>() 
               where profile.LastName.Equals("Zafar") 
               let diffYears = DbFunctions.DiffYears(profile.Birthdate, DateTime.Today) 
               select profile; 
     return query.ToList(); 
    } 

我得到的,因爲DbFunctions.DiffYears例外:

Exception Details: System.MissingMethodException: No parameterless constructor defined for this object. 

我得到一個記錄,如果我沒有在上面的查詢使用DbFunctions。我想知道如何在搜索索引時使用DbFunctions。這只是一個示例查詢,我的唯一目標就是在標題中提到的。

ProfileSearchItem類:

public class ProfileSearchItem 
    { 
     [IndexField("_group")] 
     public string ItemId { get; set; } 

     [IndexField("_language")] 
     public string Language { get; set; } 

     [IndexField("birthdate")] 
     public DateTime Birthdate { get; set; } 

     [IndexField("first_name")] 
     public string FirstName { get; set; } 

     [IndexField("last_name")] 
     public string LastName { get; set; } 
    } 

回答

0

我不認爲查詢想起來就裏面和查詢分析程序是試圖將其轉換成一些功能。

在你的情況下,你可以做POCO查詢後的年差的計算。

在您的ProfileSearchItem POCO上有一個屬性;

public int YearsDifference { get { return Datetime.Now.Year - Birthdate.Year;} }