7

我怎样才能做到这query一点LINQ and LAMBDA

询问

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桌子?

4

2 回答 2

6

您隐藏San_Proposta在名为 的字段中San_Negocio,因此调用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
                 });
于 2012-11-14T20:03:12.233 回答
5

这是一个正确的 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。

于 2012-11-14T19:10:01.943 回答