一个项目可以有多个 greenQualityEffects。为此,我把它看成是一种 NoSQL 数据库结构,并把它返回成一个 DTO 对象。
总结;
我想要做的是提取一个项目拥有的所有 GreenQualityEffect 数据并将其返回到这个 DTO 对象中。 (我正在使用 MS SQL,但表之间没有关系。就像认为它们不是 SQL)
这是我的处理程序类:
var list = new List<GreenQualityEffect>();
await _itemBusinessRules.ItemShouldBeListedWhenSelected(request.PageRequest.Page, request.PageRequest.PageSize);
IPaginate<Item> activeList = await _itemService.GetActiveGreenQualityItemList(request.PageRequest.Page, request.PageRequest.PageSize);
GreenQualityItemListModel mappedGreenQualityItemListModel = _mapper.Map<GreenQualityItemListModel>(activeList);
foreach (var item in mappedGreenQualityItemListModel.Items)
{
var greenItem = await _itemService.GetById(item.Id);
var greenQuality = await _greenQualityService.GetById(greenItem.GreenQualityId);
IPaginate<GreenQualityEffect> greenQualityEffectList = await _greenQualityEffectService.GetListByGreenQualityId(greenQuality.Id);
item.GreenQualityName = greenQuality.Name;
item.GreenQualityDescription= greenQuality.Description;
foreach (var item2 in greenQualityEffectList.Items)
{
foreach (var item3 in item.EffectItems)
{
item3.EffectName = item2.EffectName;
item3.EffectDescription= item2.EffectDescription;
}
}
}
return mappedGreenQualityItemListModel;
这是我的 DTO :
public class GreenQualityItemListModel
{
public IList<GreenQualityItemDto> Items { get; set; }
}
这是 GreenQualityItemDto :
public class GreenQualityItemDto
{
//Item
public int Id { get; set; }
public string Name { get; set; }
public string Description { get; set; }
public double Cost { get; set; }
public double SellPrice { get; set; }
public string Tag { get; set; }
public QualityType QualityType { get; set; }
public ItemCategory ItemCategory { get; set; }
public ItemType ItemType { get; set; }
public ItemHeroType ItemHeroType { get; set; }
public int GreenQualityId { get; set; }
public bool? IsStackable { get; set; }
public bool? IsConsumable { get; set; }
public bool? IsGlpyh { get; set; }
//ItemGreenQuality
public string GreenQualityName { get; set; }
public string GreenQualityDescription { get; set; }
public ICollection<GreenQualityItemEffectDto> EffectItems { get; set; }
public GreenQualityItemDto()
{
EffectItems = new HashSet<GreenQualityItemEffectDto>();
}
}
这是我要返回的DTO模型:
{
"id": 1,
"name": "New Item New",
"description": "Item Desc New One",
"greenQualityId": 2,
"greenQualityName": "Test",
"greenQualityDescription": "Test",
"effectItems": []
}
我尝试将列表与许多 linq 查询和 Foreach 构造同步,但它没有用。