2017-12-18 227 views
0

我正在使用wcf服務轉換爲角色js web應用程序。我有兩個表。我將兩個表記錄加入單個記錄並在角度js應用程序中顯示記錄。當我有記錄兩個表,它能夠檢索,但如果我只有一個數據庫表記錄,它不同意這種下面的查詢顯示任何東西..聯合運營商Linq查詢返回錯誤

public string TranscationDetails(string Account_Number) 
     { 

      var accountNumber = int.Parse(Account_Number);//It could be better to use TryParse 
      using (HalifaxDatabaseEntities context = new HalifaxDatabaseEntities()) 
      { 
       var CombinedQuery = (from x in context.Current_Account_Deposit 
            join y in context.Current_Account_Withdraw 
            on x.Account_Number equals y.Account_Number 
            where x.Account_Number == accountNumber //Modify it 
            select new 
            { 
             x.Account_Number, 
             x.Account_Holder_Name, 
             x.Transcation_Type, 
             x.Amount, 
             Transcation_Type1 = y.Transcation_Type, 
             Amount1 = y.Amount, 

             // put other properties here 
            }).ToList(); 

       var js = new System.Web.Script.Serialization.JavaScriptSerializer(); 

       return js.Serialize(CombinedQuery); // return JSON string 
      } 

     } 
    } 
} 

這裏是數據庫記錄。 enter image description here

這裏是當我運行應用程序並輸入帳號1時的屏幕截圖,它的表格和它的顯示都有記錄。 enter image description here

這裏是當我輸入賬號15只爲Current_Account_Withdraw表記錄,沒有對Current_Account_Deposit表記錄其不顯示任何內容的截屏。 enter image description here

然後我改變它到這個。

public string TranscationDetails(string Account_Number) 
    { 

     var accountNumber = int.Parse(Account_Number);//It could be better to use TryParse 
     using (HalifaxDatabaseEntities context = new HalifaxDatabaseEntities()) 
     { 
      var DepsoitQuery = (from x in context.Current_Account_Deposit 
           join y in context.Current_Account_Withdraw 
           on x.Account_Number equals y.Account_Number 
           into JoinedList 
           from y in JoinedList.DefaultIfEmpty() 
           where x.Account_Number == accountNumber //Modify it 
           select new 
           { 
            x.Account_Number, 
            x.Account_Holder_Name, 
            x.Amount, 

            // put other properties here 
           }).ToList(); 

      var withdrawQuery = (from y in context.Current_Account_Deposit 
           join x in context.Current_Account_Withdraw 
           on y.Account_Number equals x.Account_Number 
           into JoinedList 
           from x in JoinedList.DefaultIfEmpty() 
           where x.Account_Number == accountNumber 
           select new 
           { 
            y.Amount, 
           }).ToList(); 

      var CombinedQuery = DepsoitQuery.Union(withdrawQuery).ToList();//**error on this line** 

      var js = new System.Web.Script.Serialization.JavaScriptSerializer(); 

      return js.Serialize(CombinedQuery); // return JSON string 
     } 

    } 

這是我編譯時得到的錯誤。 '列出<>' 不包含 '聯盟' 的定義和最佳的擴展方法過載 'Queryable.Union <>(IQueryable的<>,IEnumerable的<>)' 需要類型的接收器 '的IQueryable <>'

回答

1

DepositQuery從這個匿名類型生成的列表:

select new 
{ 
    x.Account_Number, 
    x.Account_Holder_Name, 
    x.Amount, 
} 

withdrawQuery從這個匿名類型生成的列表:

select new 
{ 
    y.Amount, 
} 

這些類型不兼容。 (你不能「聯合」一個字符串和一個整數,例如,或者兩個單獨的類 - 當你有兩個不同的匿名類型時,這就是你正在做的事情)你需要讓兩種類型聲明相同的屬性鍵入,如果你想能夠一起使用它們。

例如,你可以聲明第二個匿名類型:

select new 
{ 
    Account_Number = 0, 
    Account_Holer_Name = "Unknown", 
    y.Amount, 
} 

您的實際解決方案將取決於什麼值,你真的想默認。

+0

看看我的第一個查詢,我沒有得到我的預期結果,然後我試着這樣 – Mohammad