选择所有类型的记录

时间:2013-05-06 15:27:09

标签: c# nosql ravendb

我正在尝试使用C#从RavenDB获取特定类型的所有记录。

当我使用Lucene时:

 var serviceTraces = session.Advanced.LuceneQuery<ServiceTrace>("IDLoadDateIndex").Take(50);

我收到了结果:

serviceTraces.QueryResult.Results

当我不使用Lucene时:

var serviceTraces = session.Query<ServiceTrace>("IDLoadDateIndex").Take(50);

我没有得到任何结果,并且在尝试在“serviceTraces”对象上执行“ToList()”时抛出异常。

为什么?

更新

ServiceTrace类:

public class ServiceTrace
    {
        public ServiceTrace(ServiceDeployment sd)
        {
            // TODO: Complete member initialization
            this.ServiceDeploymentID = sd.Id;
        }

        public string Id { get; set; }
        public string TransactionID { get; set; }
        public string ParentTransactionID { get; set; }
        public string RequestID { get; set; }
        public int ApplicationCode { get; set; }
        public int InstituteCode { get; set; }
        public string ServiceDeploymentID { get; set; }
        public string UserHostAddress { get; set; }
        public string UserAgent { get; set; }
        public string Username { get; set; }
        public DateTime RequestDateTime { get; set; }
        public DateTime ResponseDateTime { get; set; }
        public string RequestBody { get; set; }
        public string ResponseBody { get; set; }
        public string Key1Value { get; set; }
        public string Key2Value { get; set; }
        public string Key3Value { get; set; }
        public string Key4Value { get; set; }
        public string Key5Value { get; set; }
        public int StatusCode { get; set; }
        public string StatusDescription { get; set; }
        public string FullExceptionText { get; set; }
        public DateTime LoadDate { get; set; }
        public DateTime ActivationDateTime { get; set; }
        public string HostAddress { get; set; }
        public string BpmID { get; set; }
        public DateTime PreProcessDatetime { get; set; }
        public string DestHostAddress { get; set; }
        public string ArchivePath { get; set; }
        public string BTInstanceID { get; set; }
        public string Temp1 { get; set; }
        public string ExternalComponentDuration { get; set; }
        public string SQLIdentity { get; set; }
        public string ExceptionCode { get; set; }
        public string CertificateID { get; set; }
        public string ExternalComponentType { get; set; }
        public string ActivationID { get; set; }
    }

IDLoadDateIndex:

public class IDLoadDateIndex : AbstractIndexCreationTask<ServiceTrace>
    {
        public IDLoadDateIndex()
        {
            Map = serviceTrace => from st in serviceTrace
                                  select new { LoadDate = st.LoadDate };
            Index(x => x.LoadDate, FieldIndexing.Analyzed);
        }
    }

2 个答案:

答案 0 :(得分:1)

解决方案是添加默认构造函数:

public ServiceTrace()
{
}

因为从RavenDB获取数据时,引擎会通过以下方式初始化一个新实例:

public ServiceTrace(ServiceDeployment sd)
        {
            // TODO: Complete member initialization
            this.ServiceDeploymentID = sd.Id;
        }

,ServiceDeployment对象是空引用。

答案 1 :(得分:0)

在这两种情况下,您都需要在serviceTraces上调用ToList