2010-12-08 65 views
0

我需要下面的SQL查詢轉換爲LINQ查詢轉換一個SQL查詢到的LINQ to SQL指令

select * 
from bancos 
where codigobanco in 
    (select distinct codigobanco 
    from headerarquivo 
    where (MONTH(datahorageracao)=12) and (YEAR(datahorageracao)=2010)) 

我怎麼能這樣做的LINQ查詢轉換這個SQL查詢?

回答

2

假設datahorageracao是一個日期時間你沒有張貼的屬性或域模型,但想你想是這樣的:

from banco in bancos 
let queryauxiliar = (from arquivo in headerarquivo where (arquivo.datahorageracao.Month = 12) && arquivo.datahorageracao.Year = 2010 select arquivo.codigobanco) 
where queryauxiliar.Any((val)=>banco.codigobanco == val.codigobanco) 
select new Banco() 
{ 
    NomeProp = banco.valor 
} 
+0

我明白了在ouroDB.HeaderArquivoes \t \t \t \t \t。凡(H => h.DataHoraGeracao.Month == dtpGeracao.Value.Month) \t \t使用LINQ到實體 \t VAR淺灘=(從公頃\t \t \t。凡(H => h.DataHoraGeracao.Year == dtpGeracao.Value.Year) \t \t \t \t從公元前ouroDB.Bancos \t \t \t(b => b.CodigoBanco.Contains(ha.CodigoBanco)) \t \t \t \t select bc).Distinct()。OrderBy(b => b.NomeBanco); – Ederaldo 2010-12-09 17:32:16

1

假設datahorageracao的類型是DateTime.Two不同的方式,你可以寫LINQ代碼首先使用lambda expressions和第二使用query expression語法兩種方法都可行 -

//Lambda Expression way get distinct codigobanco... 
var codigobancolist =Context.headerarquivo.Where(c=>c.datahorageracao.Month ==12 && c.datahorageracao.Year==2010).Select(c=>c.codigobanco).Distinct(); 

//Query Expression way to get actual data... 
    var data= from b in bancos 
       where b.codigobanco.Contains(codigobancolist) 
       select b; 
0
var banco = from c in bancos 
      where headerarquivos 
        .Select(o => o.codibanco) 
        .Where(o.MONTH(datahorageracao)=12) and (o.YEAR(datahorageracao)=2010)) 
        .Contains(c.codibanco) 
      select c; 

敢肯定,應該做的我噸。

+1

您需要將`Select(o => o.codibanco)``一行下移 – 2010-12-08 17:57:51