2 つのモデル calss ユーザーと製品があり、共通のプロパティ productId があります。Mongodb からユーザー データを取得したい。表示されるべき彼の下の製品に対応するユーザーコアを取得したい。
ユーザーモデル
public class User : 製品 {
[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;
}
}
}
}