2013-05-08 97 views
1

我得在編輯page.For選擇複選框,我有這樣的查詢:錯誤查詢:LINQ到實體無法識別方法「System.String的ToString()」方法

var queryFI=(from u in _db.User where u.UserID==id where u.IsActive==1 
          select u); 
      var join_queryFI=from r in queryFI join f in _db.Financial on r.FinancialID equals f.FinancialID 
          into c 
          from d in c.DefaultIfEmpty() 
          select new viewpartial 
          { 
           Text = d.FiName, 
           Value = d.FinancialID.ToString(), 
           Selected = d.FinancialID == r.FinancialIntermediaryID ? true : false        
          }; 

      ViewBag.IfcList = join_queryFI.ToList(); 

我得到了一個錯誤:

LINQ to Entities does not recognize the method 'System.String ToString()' method, and this method cannot be translated into a store expression 

請幫助

回答

1

可以使用SqlFunctions.StringConvert方法,而不是ToString

試試這一次,SqlFunctions.StringConvert(d.FinancialID)

+0

假設'd.FinancialID'是proabably一個int,你必須轉換爲雙精度或十進制(因爲'SqlFunctions.StringConvert'沒有以int爲參數的重載):so'SqlFunctions.StringConvert((double)d.FinancialId)' – 2013-05-08 08:55:13

1

你可以執行查詢後轉換爲字符串:

var queryFI = (from u in _db.User 
       where u.UserID == id 
       where u.IsActive == 1 
       select u); 
var join_queryFI = from r in queryFI 
        join f in _db.Financial on r.FinancialID equals f.FinancialID 
         into c 
        from d in c.DefaultIfEmpty() 
        select new viewpartial 
        { 
         Text = d.FiName, 
         Value = d.FinancialID, 
         Selected = d.FinancialID == r.FinancialIntermediaryID ? true : false 
        }; 

ViewBag.IfcList = join_queryFI.ToList().Select(x => new { Text = x.Text, Value = x.Value.ToString(), Selected = x.Selected }).ToList(); 
相關問題