我在订单和标签之间没有直接联系,因此我通过与订单具有多对多关系并且与标签具有多对多关系的文章进行了研究。一个标签可以有多个变量。
当我执行此查询时,我想获取标签和与其连接的所有文章的列表。有点相反的方式。
我得到的结果有点奇怪。它会复制标签两次或更多次,具体取决于连接到指定订单的商品数量
var returnedList = await _unitOfWork.Repository<Order>().FindByIncludeAsync(x => x.OrderId == id,
g =>
g.Include(p => p.OrderArticles)
.ThenInclude(t => t.Article)
.ThenInclude(h => h.LabelArticles)
.ThenInclude(j => j.Label)
.ThenInclude(f => f.Variables));
var result = returnedList.Select(x => new
{
x.OrderId,
x.OrderNumber,
OrderInfo = x.OrderArticles.Select(t => new
{
Labels = t.Article.LabelArticles.Select(h => new
{
h.Label.LabelId,
h.Label.Name,
EditableVariables = h.Label.Variables.Where(l => l.VariableEditType == VariableEditType.Editable).Select(m => new
{
m.VariableId,
m.VariableName,
m.PropertySize
}),
ConnectedArticles = h.Label.LabelArticles.Select(e => new
{
e.Article.ArticleId,
e.Article.Name
})
}).GroupBy(z => z.LabelId).Select(o => o.FirstOrDefault())
}).FirstOrDefault()
}).ToList();
return Ok(result);
当前结果:
[
{
"orderId": 1,
"orderNumber": "42253",
"orderInfo": [
{
"label": {
"labelId": 1,
"name": "Patient.nlbl",
"editableVariables": [
{
"variableId": 1,
"variableName": "Hospital",
"propertySize": "30"
},
{
"variableId": 3,
"variableName": "Sex",
"propertySize": "1"
},
{
"variableId": 5,
"variableName": "First name",
"propertySize": "30"
},
{
"variableId": 7,
"variableName": "Blood type",
"propertySize": "11"
}
],
"connectedArticles": [
{
"articleId": 20,
"name": "Things"
},
{
"articleId": 21,
"name": "More things"
}
]
}
},
{
"label": {
"labelId": 1,
"name": "Patient.nlbl",
"editableVariables": [
{
"variableId": 1,
"variableName": "Hospital",
"propertySize": "30"
},
{
"variableId": 3,
"variableName": "Sex",
"propertySize": "1"
},
{
"variableId": 5,
"variableName": "First name",
"propertySize": "30"
},
{
"variableId": 7,
"variableName": "Blood type",
"propertySize": "11"
}
],
"connectedArticles": [
{
"articleId": 20,
"name": "Things"
},
{
"articleId": 21,
"name": "More things"
}
]
}
},
{
"label": {
"labelId": 3,
"name": "Report.nlbl",
"editableVariables": [
{
"variableId": 11,
"variableName": "TotalPrice",
"propertySize": "10"
},
{
"variableId": 13,
"variableName": "Qty",
"propertySize": "10"
},
{
"variableId": 15,
"variableName": "ID",
"propertySize": "10"
},
{
"variableId": 17,
"variableName": "BillTo",
"propertySize": "20"
}
],
"connectedArticles": [
{
"articleId": 22,
"name": "article1"
},
{
"articleId": 23,
"name": "Robot"
}
]
}
},
{
"label": {
"labelId": 3,
"name": "Report.nlbl",
"editableVariables": [
{
"variableId": 11,
"variableName": "TotalPrice",
"propertySize": "10"
},
{
"variableId": 13,
"variableName": "Qty",
"propertySize": "10"
},
{
"variableId": 15,
"variableName": "ID",
"propertySize": "10"
},
{
"variableId": 17,
"variableName": "BillTo",
"propertySize": "20"
}
],
"connectedArticles": [
{
"articleId": 22,
"name": "article1"
},
{
"articleId": 23,
"name": "Robot"
}
]
}
}
]
}
]
因此,如您所见,我得到了LabelId 1和3的重复。我认为这取决于一个事实,即与订单关联的商品数量将决定重复发生的次数。
我试图按LabelId将结果分组,然后选择firstorDefault,但仍然得到重新排列。
我如何摆脱它们?