2012-12-11 115 views
0

如何使用LAMBDA Expressions轉換LINQ查詢?LINQ與Lambda表達式

var select = from si in db.San_Imovel 
       join sic in db.San_Imovel_caracteristica 
       on si.Imovel_Id equals Convert.ToInt64(sic.Imovel_Id) 
       join sf in db.San_Filial 
       on si.Credenciada_Id equals sf.Credenciada_Id 
       where si.Credenciada_Id == credenciada_Id 
       && (si.GrupoImovel_Id.ToString().Contains("1") || si.GrupoImovel_Id.ToString().Contains("2")) 
       && (si.Status_Id.ToString().Contains("1") || si.Status_Id.ToString().Contains("12")) 
       && si.NomeArquivo != null 
       && (si.Imovel_Id.ToString().Contains("")) 
       select new 
       { 
        si.Celula_Id, 
        si.Credenciada_Id, 
        si.Imovel_Id, 
        si.NomeArquivo, 
        si.TipoDsc1, 
        si.BairroDsc1, 
        si.AreaRealPrivativa, 
        sic.VagasGaragem, 
        si.ValorImovel, 
        si.ValorCondominio, 
        si.ValorIPTU, 
        si.Lat2, 
        si.Lon2, 
        sf.ApelidoCredenciada, 
        sf.ddd, 
        sf.TelefoneVenda, 
        sf.TelefoneLocacao, 
        sf.Email, 
        si.Bairro1, 
        si.NomeCidade, 
        si.Transacao_ID 
       }; 
+0

你爲什麼想要?在兩次加入之後,它最終會變得難以閱讀。 –

+0

這是因爲我需要像Scot Gu的博客http://weblogs.asp.net/scottgu/archive/2008/01/07/dynamic-linq-part-1-using-the- LINQ的動態查詢library.aspx –

回答

1
var query = 
    db.San_Imovel 
    .Join(db.San_Imovel_caracteristica, 
      si => Imovel_Id, 
      sic => Convert.ToInt64(sic.Imovel_Id), 
      (si, sic) => new { si, sic }) 
    .Join(db.San_Filial, 
      x => x.si.Credenciada_Id, 
      sf => sf.Credenciada_Id, 
      (x, sf) => new { x.si, x.sic, sf }) 
    .Where(x => x.si.Credenciada_Id == credenciada_Id && 
       (x.si.GrupoImovel_Id.ToString().Contains("1") || 
        x.si.GrupoImovel_Id.ToString().Contains("2")) && 
       (x.si.Status_Id.ToString().Contains("1") || 
        x.si.Status_Id.ToString().Contains("12")) && 
        x.si.NomeArquivo != null && 
       (x.si.Imovel_Id.ToString().Contains("")) 
    .Select(x => new { 
        x.si.Celula_Id, 
        x.si.Credenciada_Id, 
        x.si.Imovel_Id, 
        x.si.NomeArquivo, 
        x.si.TipoDsc1, 
        x.si.BairroDsc1, 
        x.si.AreaRealPrivativa, 
        x.sic.VagasGaragem, 
        x.si.ValorImovel, 
        x.si.ValorCondominio, 
        x.si.ValorIPTU, 
        x.si.Lat2, 
        x.si.Lon2, 
        x.sf.ApelidoCredenciada, 
        x.sf.ddd, 
        x.sf.TelefoneVenda, 
        x.sf.TelefoneLocacao, 
        x.sf.Email, 
        x.si.Bairro1, 
        x.si.NomeCidade, 
        x.si.Transacao_ID 
       }); 

BTW你不需要整個查詢轉換方法的語法。您只能轉換過濾where部分。