我需要在表格中查询两个重复列中的值

时间:2019-05-04 22:37:53

标签: c# asp.net linq asp.net-core entity-framework-core

我正在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}返回给我,因为它们具有相同的用户AND定义ID。 我已经尝试使用Groupby来提供这两个值,但是我无法获得IQueriable或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;

2 个答案:

答案 0 :(得分:0)

尝试一下

var placementsWithDuplicates = from p in _context.Placements.Where(m => m.User_id == m.Definition_id)
select new {
    Id = p.Id,
    User = p.User_id,
    Defi = p.Definition_id,
};

// this is same as the top one
var placementsWithDuplicates = from p in _context.Placements where p.User_id == p.Definition_id
select new {
    Id = p.Id,
    User = p.User_id,
    Defi = p.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!"
        });
};

答案 1 :(得分:0)

请在下面的SQL查询中找到

    SELECT A.* 
    FROM Placements A 
    INNER JOIN 
     (SELECT User_id, Definition_id FROM Placements 
      GROUP BY User_Id, Definition_id 
      HAVING COUNT(*) > 1) B 
     ON A.User_id = B.User_id AND A.Defination_id = 
     B.Defination_id

您可以创建一个临时表以避免连接子查询。

如果您想使用linq查询,那么我尝试通过上述查询创建它,请在下面找到它:

 --- sub query
 var B = from p in Placements 
 group p by new { p.User_id, p.Definition_id } into what 
 where what.count() > 1
 select new
   { User_id = what.User_id, 
     Definition_id =what.Definition_id 
    }; 

    --- join
    Var result =  from A in Placements
                          Join B ON A.User_id = B.User_id 
                          AND A.Defination_id = B.Defination_id
                          Select A

请尝试这个。