我有一个LINQ to Entities查询,我想只返回TagID和TagName的唯一值。
代码:
var tags = (from t in _dbContext.TaskRecordDetails.ToList()
join tag in _dbContext.Tags on t.TagName equals tag.TagName
where t.Period == period
select new TagDTO()
{
TagID = tag.TagID,
TagName = tag.TagName
});
return tags.Distinct<TagDTO>();
但是,仍然会返回重复的行,如下所示。如何确保只返回唯一的行?
[
{
"TagID": 1,
"TagName": "Level 1",
"TagDescription": null,
"IsActive": false
},
{
"TagID": 2,
"TagName": "Level 3",
"TagDescription": null,
"IsActive": false
},
{
"TagID": 3,
"TagName": "Level 5",
"TagDescription": null,
"IsActive": false
},
{
"TagID": 1,
"TagName": "Level 1",
"TagDescription": null,
"IsActive": false
},
{
"TagID": 2,
"TagName": "Level 3",
"TagDescription": null,
"IsActive": false
}
]
答案 0 :(得分:1)
根据您的上一条评论,这是一个解决方案:
var tags = (from t in _dbContext.TaskRecordDetails
join tag in _dbContext.Tags on t.TagName equals tag.TagName
where t.Period == period
select tag).Distinct().ToList();
return tags.select(x => new TagDTO
{
TagID = x.TagID,
TagName = x.TagName
});