none
Problems with Linq RRS feed

  • Question

  • I am writting a query with linq(follow code bellow). I am looking for data from db.imovel and for each one bring a picture nested into db.fotoimovel. Keep in mind that I may have more than one picture for each db.imovel data.

    The issue is when I run, it returns me all the pictures with the same db.imovel data in all of them.

    It works for me if return the db.imovel data with just one(may be the first picture) from db.fotoimovel.

    var retorno = (from c in db.fotoimovel
                        join p in db.imovel on c.idImovel equals p.idImovel
                        where p.MetragemImovel >= metragem1 && p.MetragemImovel <= metragem2 &&
                        p.StatusImovel == opcao && 
                        ((String.IsNullOrEmpty(bairro)) || p.BairroImovel == bairro) &&
                        ((imovel == null) || (p.TipoImovel == imovel)) &&
                        ((carros == null) || (p.QtdVagaGaragemImovel == carros)) &&
                        ((quartos == null) || (p.QtdQuartoImovel == quartos))
                        select new DadosImovel
                        {
                            Id = p.idImovel,
                            Titulo = p.TituloImovel,
                            Descricao = p.DescricaoImovel,
                            Foto = c.NomeFotoImovel,
                            TipoImovel = p.TipoImovel,
                            Endereco = p.EnderecoImovel,
                            Complemento = p.ComplementoImovel,
                            Bairro = p.BairroImovel,
                            Cidade = p.CidadeImovel,
                            Estado = p.EstadoImovel,
                            CEP = p.CEPImovel,
                            MetragemImovel = p.MetragemImovel,
                            QtdComodoImovel = p.QtdComodoImovel,
                            QtdQuartoImovel = p.QtdQuartoImovel,
                            QtdBanheiroImovel = p.QtdBanheiroImovel,
                            QtdVagaGaragemImovel = p.QtdVagaGaragemImovel,
                            QtdAndarImovel = p.QtdAndarImovel,
                            Status = p.StatusImovel,
                            Ativo = p.AtivoImovel
                        }).Distinct().ToList();

    Thanks in advanced!!!


    []´s, Rodney Oliveira

    Wednesday, April 17, 2013 10:02 PM

Answers

  •  var retorno = (from p in db.imovel
                           where p.MetragemImovel >= metragem1 && p.MetragemImovel <= metragem2 &&
                           p.StatusImovel == opcao &&
                           ((String.IsNullOrEmpty(bairro)) || p.BairroImovel == bairro) &&
                           ((imovel == null) || (p.TipoImovel == imovel)) &&
                           ((carros == null) || (p.QtdVagaGaragemImovel == carros)) &&
                           ((quartos == null) || (p.QtdQuartoImovel == quartos))
                           select new DadosImovel
                           {
                               fotoimovels = from c in db.fotoimovel where c.idImovel = p.idImovel
    
                               Id = p.idImovel,
                               Titulo = p.TituloImovel,
                               Descricao = p.DescricaoImovel,
                               Foto = c.NomeFotoImovel,
                               TipoImovel = p.TipoImovel,
                               Endereco = p.EnderecoImovel,
                               Complemento = p.ComplementoImovel,
                               Bairro = p.BairroImovel,
                               Cidade = p.CidadeImovel,
                               Estado = p.EstadoImovel,
                               CEP = p.CEPImovel,
                               MetragemImovel = p.MetragemImovel,
                               QtdComodoImovel = p.QtdComodoImovel,
                               QtdQuartoImovel = p.QtdQuartoImovel,
                               QtdBanheiroImovel = p.QtdBanheiroImovel,
                               QtdVagaGaragemImovel = p.QtdVagaGaragemImovel,
                               QtdAndarImovel = p.QtdAndarImovel,
                               Status = p.StatusImovel,
                               Ativo = p.AtivoImovel
                           }).Distinct().ToList();



    Please Mark as Answer and Vote as Helpful if I helped.

    Also please visit my blog http://msguy.net/

    Thursday, April 18, 2013 9:51 AM