Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/294.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 如何使用IQueryable中的DbFunctions进行Lucene索引搜索_C#_Lucene_Sitecore_Sitecore8 - Fatal编程技术网

C# 如何使用IQueryable中的DbFunctions进行Lucene索引搜索

C# 如何使用IQueryable中的DbFunctions进行Lucene索引搜索,c#,lucene,sitecore,sitecore8,C#,Lucene,Sitecore,Sitecore8,我使用以下代码从Lucene索引中获取结果 using (var context = ContentSearchManager.GetIndex("my_profile_index").CreateSearchContext()) { IQueryable<ProfileSearchItem> query = from profile in context.GetQueryable<ProfileSearchItem>()

我使用以下代码从Lucene索引中获取结果

    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
,我只会得到一条记录。我想知道在搜索索引时如何使用
DbFunctions
。这只是一个示例查询,我的唯一目标如标题中所述

ProfileSearchItem
class:

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

我认为查询不喜欢在其中包含函数,而查询解析器正试图将其转换成某种东西

在您的情况下,您可以在查询POCO后计算年份差异

在ProfileSearchItem POCO上有一个属性

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

日期字段在Lucene中保留为字符串,如20170308,因此许多数据方法都不起作用,即使是比较[DateField].Year这样的简单方法也不行,因此我猜DbFunctions.DiffYears正在执行一些lambda表达式,并使用date方法填充结果

看一看,你可能会看得更清楚

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