【发布时间】:2019-09-23 00:54:26
【问题描述】:
我正在使用 ASP.NET Core。我在查询具有相同 User_id 和 Definition_id 的行时遇到问题,如果有类似的,我需要行的 ID。
+----+---------+---------------+
| Id | User-id | Definition-id |
+----+---------+---------------+
| 1 | 1 | 1 |
| 2 | 2 | 1 |
| 3 | 2 | 2 |
| 4 | 3 | 1 |
| 5 | 4 | 1 |
| 6 | 4 | 1 |
| 7 | 5 | 2 |
| 8 | 6 | 1 |
+----+---------+---------------+
我需要像这样查询表,将 { 5, 6 } 返回给我,因为它们具有相同的用户和定义 ID。
我已经为这两个值尝试了Groupby,但我无法让IQueryable 或IGrouping 给我特定行的ID。
我想它会这样工作,但事实并非如此。
var placementsWithDuplicates =
from p in _context.Placements
group p by new { p.User_id, p.Definition_id } into what
select new
{
Id = what.Id,
User = what.User_id,
Defi = what.Definition_id,
};
foreach (var p in placementsWithDuplicates)
{
issues.Add(new IssueModel()
{
Type = "Praxe ID[" + p.Id + "]",
Name = "User id [" + p.User + "], Definition id [" + p.Defi + "]",
Detail = "So user shouldnt have data for more definitons!"
});
};
感谢 Satish Hirpara 的最佳答案,它需要一些更新,所以我发布了最终运行良好的东西:
var B = from p in _context.Placements
group p by new { p.User_id, p.Definition_id } into what
where what.Count() > 1
select new
{
User_id = what.Key.User_id,
Definition_id = what.Key.Definition_id
};
var placementsWithDuplicates = from A in _context.Placements
join b in B on new { A.User_id, A.Definition_id } equals new { b.User_id, b.Definition_id }
select A;
【问题讨论】:
标签: c# asp.net linq asp.net-core entity-framework-core