C# 带有子where子句的LINQ查询
我有一个SQL查询:C# 带有子where子句的LINQ查询,c#,linq,C#,Linq,我有一个SQL查询: SELECT [Paypoint] ,[Department] ,[EmployeeCode] ,[Gender] ,[EmployeeTitle] ,[Initials] ,[Surname] ,[ItemsIssuedDate] ,[ItemsIssuedStockNumber] FROM [MyTable] AS a WHERE ( [ItemsIs
SELECT [Paypoint]
,[Department]
,[EmployeeCode]
,[Gender]
,[EmployeeTitle]
,[Initials]
,[Surname]
,[ItemsIssuedDate]
,[ItemsIssuedStockNumber]
FROM [MyTable] AS a
WHERE
(
[ItemsIssuedDate] = ( SELECT max([ItemsIssuedDate])
FROM [MyTable] AS b
WHERE a.[Paypoint] = b.[Paypoint]
AND a.[Department] = b.[Department]
AND a.[EmployeeCode] = b.[EmployeeCode]
AND a.[Gender] = b.[Gender]
AND a.[Surname] = b.[Surname]
)
如何获得比较LINQ查询?我不能使用SQL查询,因为数据已经在数据集中,现在需要进一步修改
我已尝试过,但这不起作用:
var query = from a in excelTable
where
(
from c in excelTable
group c by new
{
c.Paypoint,
c.EmployeeCode
} into g
where string.Compare(a.Paypoint, g.Key.Paypoint) == 0 && string.Compare(a.EmployeeCode, g.Key.Paypoint) == 0
select g.Key.Paypoint
)
select a;
您还可以选择仅包含必填字段的匿名对象。取决于您。您对SQL查询最直接的操作是:
var query =
from a in excelTable
let maxIssueDate =
(from b in excelTable
where a.Paypoint == b.Paypoint &&
a.Department == b.Department &&
a.EmployeeCode == b.EmployeeCode &&
a.Gender == b.Gender &&
a.Surname == b.Surname
select b.ItemsIssueDate).Max()
where a.ItemsIssueDate == maxIssueDate
select a;
不幸的是,这只返回一个项目,每个日期,如果有许多项目与同一日期,它不工作…谢谢,这是工作-任何方式使它更快。。。。在backgroundworker中运行查询大约需要6分钟。。。
var query =
from a in excelTable
let maxIssueDate =
(from b in excelTable
where a.Paypoint == b.Paypoint &&
a.Department == b.Department &&
a.EmployeeCode == b.EmployeeCode &&
a.Gender == b.Gender &&
a.Surname == b.Surname
select b.ItemsIssueDate).Max()
where a.ItemsIssueDate == maxIssueDate
select a;