我有两个模型类用户和产品,并且我有共同的属性 productId。我想从 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;
}
}
}
}