我是Raven的新手,所以我甚至不確定我是否在這裏採取正確的方法。我需要生成一個項目列表,其屬性是從單獨的文檔中提取的...本質上是一個JOIN。在RavenDB Multi Map中遇到麻煩reduce
說我有這些對象:
public class Merchant
{
public string Id {get;set;}
public string MerchantName {get;set;}
}
public class Location
{
public string Id {get;set;}
public string MerchantId {get;set;}
public string City {get;set;}
}
public class Item
{
public string Id {get;set;}
public string MerchantId {get;set;}
public string ItemName {get;set;}
}
,我需要的MapReduce(我想)到MerchantItem
public class MerchantItem
{
public string ItemId {get;set;}
public string MerchantId {get;set;{
public string LocationId {get;set;}
public string ItemName {get;set;}
public string City {get;set;}
public string MerchantName {get;set;}
}
這裏是我的(非工作)多地圖縮小的列表:
AddMap<Merchant>(merchants => from m in merchants
select new
{
ItemId = (string)null,
MerchantId = m.Id,
LocationId = (string)null,
ItemName = (string)null,
City = (string)null,
MerchantName = m.Name
});
AddMap<Location>(locations => from l in locations
select new
{
ItemId = (string)null,
MerchantId = l.MerchantId,
LocationId = l.Id,
ItemName = (string)null,
City = l.City,
MerchantName = (string)null
});
AddMap<Item>(items=> from i in items
select new
{
ItemId = i.Id,
MerchantId = i.MerchantId,
LocationId = (string)null,
ItemName = i.ItemName,
City = (string)null,
MerchantName = (string)null
});
Reduce = results => from r in results
group result by r.LocationId
into g
select new
{
ItemId = g.Select(x => x.ItemId),
MerchantId = g.Select(x => x.MerchantId),
LocationId = g.Key,
ItemName = g.Select(x => x.ItemName).Where(x => x != null).FirstOrDefault(),
City = g.Select(x => x.City).Where(x => x != null).FirstOrDefault(),
MerchantName = g.Select(x => x.MerchantName).Where(x => x != null).FirstOrDefault()
});
這個想法是我需要一個MerchantItem用於數據庫中的每個Item和Location。在SQL中,我們只是在談論幾個連接,但我完全難以理解如何在Raven中實現這一點。
也許使用Raven的Include()功能作爲不同的(更好的?)方法?我在回答這個問題時提供了一個例子:http://stackoverflow.com/questions/9500043/query-list-of-sub-documents-with-ravendb – 2012-03-05 04:38:13
哈,這也是我的問題,是的類似的問題。我只是剛纔看到你已經回答了。我會給你一個辦法。謝謝。 – 2012-03-05 18:05:32
大聲笑。我也注意到了。我在那個答案中描述的是Ayende在下面說的:Include()。 – 2012-03-05 18:18:43