Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/linq/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# LINQ:如何选择具有多列的唯一行:max(id)?_C#_Linq_Group By - Fatal编程技术网

C# LINQ:如何选择具有多列的唯一行:max(id)?

C# LINQ:如何选择具有多列的唯一行:max(id)?,c#,linq,group-by,C#,Linq,Group By,这是一个表的玩具示例,该表有许多列和1000行 我希望筛选出包含相同AcctNo、CustomerName和CustomerContact的所有行,但保留其中一个重复项的ID,以便以后可以访问该记录 例如: ID AcctNo CustomerName CustomerContact 1 1111 Acme Foods John Smith 2 1111 Acme Foods John Smith 3 1111 Acme Foods Judy

这是一个表的玩具示例,该表有许多列和1000行

我希望筛选出包含相同AcctNo、CustomerName和CustomerContact的所有行,但保留其中一个重复项的ID,以便以后可以访问该记录

例如:

ID  AcctNo  CustomerName  CustomerContact
1   1111    Acme Foods    John Smith
2   1111    Acme Foods    John Smith
3   1111    Acme Foods    Judy Lawson
4   2222    YoyoDyne Inc  Thomas Pynchon
5   2222    YoyoDyne Inc  Thomas Pynchon
<= For AcctNo 1111, I want to save IDs 2 and 3
确定:返回ID 2和3:

ID AcctNo CustomerName CustomerContact
-- ------ ------------ ---------------
2  11111  Acme Foods   John Smith
3  11111  Acme Foods   Judy Lawson
问:这个SQL的LINQ等价物是什么

失败的尝试:

IQueryable<CustomerData> query =
    from c in context.CustomerData
    where c.AcctNo == acctno
    group c by new { c.AcctNo , c.CustomerName, c.CustomerContact } into gcs
    select new { newID = gcs.Max(x => x.ID), gcs.AcctNo, gcs.CustomerName, gcs.CustomerContact }

首先创建一个与此类似的自定义DTO视图模型

public CustomerViewModel
{
   public string AcctNo { get; set; }
   public string CustomerName { get; set; }
   public string CustomerContact { get; set; }
   public int MaxId { get; set; }
}       
询问

context.CustomerData.Where(c => c.AcctNo == "1111")
       .GroupBy(c => new { c.AcctNo , c.CustomerName, c.CustomerContact })
       .Select(cg => new CustomerViewModel {
                      AcctNo  = cg.Key.AcctNo,
                      CustomerName  = cg.Key.CustomerName,
                      CustomerContact  = cg.Key.CustomerContact,
                      MaxId = cg.Max(c => c.ID)
        })

你几乎把它做好了。只需使用投影中的键访问组属性

IQueryable<CustomerData> query =
    from c in context.CustomerData
    where c.AcctNo == acctno
    group c by new { c.AcctNo , c.CustomerName, c.CustomerContact } into gcs
    select new { newID = gcs.Max(x => x.ID), gcs.Key.AcctNo, gcs.Key.CustomerName, gcs.Key.CustomerContact }

1我丢失的部分是c.Key.xxx。2在我的实际程序中,我想做的是将一个结果传递给另一个ASP.Net核心页面。我正在考虑将行序列化为JSON,然后用JSON字符串调用新页面。你的DTO想法可以简化这一点。两方面都谢谢你!太好了,谢谢!
IQueryable<CustomerData> query =
    from c in context.CustomerData
    where c.AcctNo == acctno
    group c by new { c.AcctNo , c.CustomerName, c.CustomerContact } into gcs
    select new { newID = gcs.Max(x => x.ID), gcs.Key.AcctNo, gcs.Key.CustomerName, gcs.Key.CustomerContact }