【发布时间】:2021-07-04 17:02:37
【问题描述】:
这是一个表的“玩具”示例,它有许多列和数千行。
我希望 FILTER OUT 任何包含相同 AcctNo、CustomerName 和 CustomerContact 的行,但保留 ONE 重复项的 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 -
工作 SQL:
select max(id) as ID,AcctNo,CustomerName,CustomerContact from test where AcctNo = '11111' group by AcctNo,CustomerName,CustomerContactOK:返回 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 }
【问题讨论】: