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
从SQL到LINQ_Sql_Linq_Entity Framework 4 - Fatal编程技术网

从SQL到LINQ

从SQL到LINQ,sql,linq,entity-framework-4,Sql,Linq,Entity Framework 4,我正试图找出如何将这个SQLSELECT转换为LINQ,但我还没有找到答案 我得到每个Id(PersonId)的最后一次测试记录,每个测试,每个月,每年。基本上,每年为每个人计算当月的最后分数 CREATE TABLE #MyTable ( Id INT, Score INT, TestName VARCHAR(50), TestDate DATETIME ) INSERT INTO #MyTable VALUES (1, 10, 'Math', '2011-12

我正试图找出如何将这个SQLSELECT转换为LINQ,但我还没有找到答案

我得到每个Id(PersonId)的最后一次测试记录,每个测试,每个月,每年。基本上,每年为每个人计算当月的最后分数

CREATE TABLE #MyTable 
(
   Id INT,
   Score INT,
   TestName VARCHAR(50),
   TestDate DATETIME
)

INSERT INTO #MyTable

VALUES

(1, 10, 'Math', '2011-12-16 00:00:00.000')

,(1, 25, 'Math', '2011-12-26 00:00:00.000')

,(1, 100, 'Math', '2011-12-06 00:00:00.000')

,(1, 10, 'Reading', '2011-12-16 00:00:00.000')

,(1, 25, 'Reading', '2011-12-26 00:00:00.000')

,(1, 100, 'Reading', '2011-12-06 00:00:00.000')

,(2, 10, 'Math', '2011-12-16 00:00:00.000')

,(2, 25, 'Math', '2011-12-26 00:00:00.000')

,(2, 100, 'Math', '2011-12-06 00:00:00.000')

,(2, 10, 'Reading', '2011-12-16 00:00:00.000')

,(2, 25, 'Reading', '2011-12-26 00:00:00.000')

,(2, 100, 'Reading', '2011-12-06 00:00:00.000')

,(1, 10, 'Math', '2011-12-16 00:00:00.000')

,(1, 25, 'Math', '2012-12-26 00:00:00.000')

,(1, 100, 'Math', '2012-12-06 00:00:00.000')

,(1, 10, 'Reading', '2012-12-16 00:00:00.000')

,(1, 25, 'Reading', '2012-12-26 00:00:00.000')

,(1, 100, 'Reading', '2012-12-06 00:00:00.000')

,(2, 10, 'Math', '2012-12-16 00:00:00.000')

,(2, 25, 'Math', '2012-12-26 00:00:00.000')

,(2, 100, 'Math', '2012-12-06 00:00:00.000')

,(2, 10, 'Reading', '2012-12-16 00:00:00.000')

,(2, 25, 'Reading', '2012-12-26 00:00:00.000')

,(2, 100, 'Reading', '2012-12-06 00:00:00.000')



SELECT DISTINCT
M.Id,M.Score,M.TestName, M.TestDate

FROM 
#MyTable M

WHERE
M.TestDate IN (
SELECT MAX(m.TestDate)
FROM #MyTable m
GROUP BY MONTH(TestDate), YEAR(TestDate)
)

DROP TABLE 
#MyTable
在子查询之前:

使用子查询后的最终结果,返回结果8条记录:

我所拥有的:

  from a in MyTable
  where   a.TestDate == from b in MyTable
   group b.TestDate.Value.Month,
        a.TestDate.Value.Year 
请注意,这将为每个月/年组合创建组,这实际上可能更适合您的业务案例。如果你需要把它弄平,你可以说:

var q2 = from g in q1
         from entry in g
         select entry;
或:

请注意,这将为每个月/年组合创建组,这实际上可能更适合您的业务案例。如果你需要把它弄平,你可以说:

var q2 = from g in q1
         from entry in g
         select entry;
或:

试试这个:

from m in myTable
where myTable
    .GroupBy(x => new { x.TestDate.Month, x.TestDate.Year })
    .Select(grp => grp.Max(x => x.TestDate))
    .Any(x => x == m.TestDate)
select new { m.Id, m.TestScore, m.TestName, m.TestDate };
试试这个:

from m in myTable
where myTable
    .GroupBy(x => new { x.TestDate.Month, x.TestDate.Year })
    .Select(grp => grp.Max(x => x.TestDate))
    .Any(x => x == m.TestDate)
select new { m.Id, m.TestScore, m.TestName, m.TestDate };

为什么在选择不同的值时,SQL查询会产生重复的条目?它们不会产生重复的条目。年份不同,主题也不同。为什么在选择不同的值时,SQL查询会产生重复的条目?它们不会产生重复的条目。年份不同,主题也不同。