【问题标题】:I need to query table for values in two duplicate columns我需要在表中查询两个重复列中的值
【发布时间】:2019-09-23 00:54:26
【问题描述】:

我正在使用 ASP.NET Core。我在查询具有相同 User_idDefinition_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,但我无法让IQueryableIGrouping 给我特定行的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


    【解决方案1】:

    请在下面找到 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
    

    请试试这个。

    【讨论】:

    • 首先,非常感谢!我使用了那个 linq 查询,但它需要一些调整,所以我更新了我的问题以包含结果。
    【解决方案2】:

    试试这个

    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!"
            });
    };
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2013-10-30
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-04-19
      • 2021-11-13
      • 2020-10-07
      相关资源
      最近更新 更多