2012-11-14 179 views
7

我該怎麼做query使用LINQ and LAMBDAMultiple Select and Join with LINQ and Lambda

QUERY

Select san_negocio.imovel_id 
     ,san_negocio.negocio_id 
     ,san_imovel.credenciada_id 
     ,san_proposta.proposta_id 
     ,san_proposta.credenciada_id 
    from san_negocio 
    join san_proposta 
    on san_negocio.imovel_id = san_proposta.imovel_id 
    join san_imovel 
    on san_negocio.imovel_id = san_imovel.imovel_id 
where san_negocio.credenciadacaptadora_id is null 
    and san_negocio.credenciadavendedora_id is null 
    and san_proposta.statusproposta_id = 2 

我已經試過:

var objetos = db.San_Negocio.Join(db.San_Proposta, a => a.Imovel_Id, b => b.Imovel_Id, (a, b) => new { San_Negocio = a, San_Proposta = b })     
    .Join(db.San_Imovel, a => a.San_Negocio.Imovel_Id, c => c.Imovel_Id, (a, c) => new { San_Negocio = a, San_Imovel = c }) 
    .Where(a => a.San_Negocio.San_Negocio.CredenciadaCaptadora_Id == null && a.San_Negocio.San_Negocio.CredenciadaVendedora_Id == null) 
    .Select(a => new { a.San_Negocio.San_Negocio.Negocio_Id, 
      a.San_Negocio.San_Negocio.Imovel_Id, 
      a.San_Imovel.Credenciada_Id }); 

我懷疑是我Select。我怎麼打電話給我的San_Proposta表?

+0

爲什麼需要它要用lambda來完成?該版本看起來絕對不好理解。 – Arran

+0

可以沒有拉姆達。這是因爲我想更好地理解lambda如何與之合作。 –

+0

我認爲這可能會更好,如果你第一次閱讀一些關於linq code.msdn.microsoft.com/101-LINQ-Samples-3fb9811b#nested的示例,然後決定你想使用什麼方法 –

回答

4

這是一個正確的LINQ聲明:

from neg in db.san_negocio 
join prop in san_proposta 
    on neg.imovel.id equals prop.imovel_id 
join imo in san_imovel 
    on neg.imovel_id = imo.imovel_id 
where neg.credenciadacaptadora_id == null && 
    neg.credenciadavendedora_id == null && 
    prop.statusproposta_id == 2 
select new { 
    ImovelID = neg.imovel_id, 
    NegocioID = neg.negocio_id, 
    Imo_CredenciadaID = imo.credenciada_id, 
    PropostaID = prop.proposta_id 
    Prop_CredenciadaID = prop.credenciada_id 
}; 

這將創建與上面列出的屬性的匿名對象的一個​​IQueryable。

+0

是的,非常感謝。但它如何使用lambda表達式工作? –

5

你是一個名爲San_Negocio領域內隱藏San_Proposta,因此調用a.San_Negocio.San_Proposta將訪問它,但我建議寫你加入的方式,字段沒有嵌套,就像這樣:

var objetos = db.San_Negocio 
    .Join(db.San_Proposta, 
      a => a.Imovel_Id, 
      b => b.Imovel_Id, 
      (a, b) => new { San_Negocio = a, San_Proposta = b })     
    .Join(db.San_Imovel, 
      a => a.San_Negocio.Imovel_Id, 
      c => c.Imovel_Id, 
      (a, c) => new { a.San_Negocio, a.San_Proposta, San_Imovel = c }) 
    .Where(a => a.San_Negocio.CredenciadaCaptadora_Id == null && 
       a.San_Negocio.CredenciadaVendedora_Id == null) 
    .Select(a => new 
       { 
        a.San_Negocio.Negocio_Id, 
        a.San_Negocio.Imovel_Id, 
        a.San_Proposta.San_Proposta_Id, 
        a.San_Imovel.Credenciada_Id 
       }); 
+1

謝謝!順便說一下。這是真正的LINQ。不是其他的東西。 – ppumkin

相關問題