将查询转换为LINQ

时间:2012-09-04 18:26:50

标签: linq lambda

如何在Linq中使用Lambda转换以下查询?

SELECT DISTINCT Registro, COUNT(Registro) as qnt 
FROM XML_Relatorio
WHERE Arquivo = 'redenet.xml' 
AND TipoErro <> 'Imovel Inserido' 
AND TipoErro <> 'TI'
AND DataHora BETWEEN '01-01-2012' AND '02-01-2012' 
GROUP BY Registro 
ORDER BY Registro

我正在尝试以下代码,但我需要一些帮助来构建LINQ with Lambda

IQueryable<XML_Relatorio> quantidadeErro = db.XML_Relatorios
    .Where(a => a.Arquivo == "redenet.xml" 
           && a.TipoErro != "Imovel Inserido" 
           && a.TipoErro != "TI");

1 个答案:

答案 0 :(得分:1)

假设DataHora字段属于DateDateTime类型。

// parse the strings to datetime 
var start = DateTime.Parse("01-01-2012");
var end = DateTime.Parse("02-01-2012");

IQueryable<XML_Relatorio> quantidadeErro = db.XML_Relatorios
   .Where(a => a.Arquivo == "redenet.xml" 
       && a.TipoErro != "Imovel Inserido" 
       && a.TipoErro != "TI"
       // and compare them...
       && a.DataHora > start && a.DataHora < end);