2013-11-21 91 views
0

我怎樣才能改變這種查詢表達式lambda表達式:將表達式查詢到Lambda語法?

var dataUser = from cm in ConsumerName 
        join c in Consumer on cm.ConsumerId equals c.Id 
        join cua in ConsumerAccount on cm.ConsumerId equals cua.ConsumerId 
        join bd in BankDetail on cm.ConsumerId equals bd.ConsumerId 
        join cpm in CardPayment on cm.ConsumerId equals cpm.ConsumerId 
        where cm.ConsumerId == consumerId 
        select new { AccountNumber=bd.AccountNumber,CardNumber= cpm.CardNumber, Name = cm.FirstName + " " + cm.MiddleName + " " + cm.LastName, Email = c.Email, AccountId = cua.AccountId }; 
+0

隨着大量匿名類型。我建議反對它。無論如何,你爲什麼要這麼做? – Aron

+0

標準評論:ORM不是SQL的替代品。如果你需要很多連接,創建一個視圖或存儲過程並映射結果會更好。或者您應該將連接轉換爲EF關聯映射 –

回答

1

簡單,自動轉換由ReSharper的

var dataUser = 
    ConsumerName.Join(Consumer, cm => cm.ConsumerId, c => c.Id, (cm, c) => new { cm, c }) 
     .Join(ConsumerAccount, @t => cm.ConsumerId, cua => cua.ConsumerId, (@t, cua) => new { @t, cua }) 
     .Join(BankDetail, @t => cm.ConsumerId, bd => bd.ConsumerId, (@t, bd) => new { @t, bd }) 
     .Join(CardPayment, @t => cm.ConsumerId, cpm => cpm.ConsumerId, (@t, cpm) => new { @t, cpm }) 
     .Where(@t => cm.ConsumerId == consumerId) 
     .Select(
      @t => 
      new 
       { 
        AccountNumber = bd.AccountNumber, 
        CardNumber = cpm.CardNumber, 
        Name = cm.FirstName + " " + cm.MiddleName + " " + cm.LastName, 
        Email = c.Email, 
        AccountId = cua.AccountId 
       }); 
+0

謝謝,它工作完美。 – user3007593