1

我创建了这个索引:

public class ReceiptsByClient : AbstractIndexCreationTask<Receipt>
    {
        public ReceiptsByClient()
        {
            Map = receipts => from r in receipts
                              let d = LoadDocument<Debtor>(r.Receipt_Debtor)
                              select new { ClientId = d.Debtor_Client };
        }
    }

现在我想查询这个索引如下:

var rptQry = Session.Query<Receipt, ReceiptsByClient>()

当然,我希望能够在 Where 子句中传递 ClientId 的值。但是 where 子句的 T 是 Receipt 类型,不知道 ReceiptsByClient 中定义的匿名类型的 ClientId。如何使用我的 ReceiptsByClient 索引进行查询?

4

1 回答 1

2

Raven在几乎所有涉及匹配查询到索引的事情上都进行Duck Typing 。您所需要的只是一个具有您要查询的字段的类型:

public class Foo
{
    public string ClientId { get; set; }
}

然后你在查询中使用它,并使用它来回避它,As这样你的结果就属于真正的类。

var q = session.Query<Foo, ReceiptsByClient>()
               .Where(x=> x.ClientId == "clients/123")
               .As<Receipt>();

Note that the ClientId field won't be in the results. If you want to do that - you have to store the field in the index with Store(x=> x.ClientId, FieldStorage.Yes) and then use AsProjection in the query to duck-type to a class that has all fields.

于 2013-03-09T05:29:51.300 回答