2
我有两个模型calss用户和产品,我有共同的属性productId。我想要从Mongodb获取用户数据。我想获取用户coreesponding他应该显示在他下面的产品。在mongodb中的数据引用
用户模型
公共类用户:产品 {
[BsonId]
public string GuId { get; set; }
public string Name { get; set; }
public string EMailAddress { get; set; }
public string ProductId { get; set; }
}
产品型号
公共类产品 {
[BsonId]
public int Id { get; set; }
public string ProductId { get; set; }
public string ProductName { get; set; }
}
而且我的应用程序层下面的代码: -
public IEnumerable<User> GetAllUsers()
{
// var users = new User();
IList<User> itemCollection = new List<User>();
var itemIdList = new List<string>();
itemCollection = _users.Include<User>.Load(itemIdList.ToArray())
.OrderByDescending(x => x.LastUpdatedDate).ToList();
// var itemCollection = _users.Load<User>(itemIdList.ToArray());
this.LoadItemStatusDefinitions(itemCollection);
var users = _users.
FindAll().
SetSortOrder(SortBy.Descending("createdate"));
return users;
}
private void LoadItemStatusDefinitions(IEnumerable<User> items)
{
// iterates through each items in the collection.
foreach (var item in items)
{
// check whether status definition is already assigned to the item
if (!string.IsNullOrEmpty(item.ProductId))
{
// loading the status definition associated with each item
var statusDefinition = _users.Load<Product>(item.ProductId);
if (statusDefinition != null)
{
// assign the status definition to the item.
item.ProductId = statusDefinition;
}
}
}
}
如果您想在单个提取中执行此操作,则不能。你需要做两个findOne调用。或者,将所有数据嵌入到单个文档中。 – WiredPrairie 2013-02-15 12:15:47