2012-07-24 47 views
5
dynamic traceFile = Environment.GetFolderPath(Environment.SpecialFolder.Desktop) + "\\EntityFrameworkTrace.log";     
var CurrentStock = (from s in DBViews.StockStatus 
        where s.ProductID != 10 
        orderby s.ProductName 
        select new 
        { 
         s.ProductID, 
         s.ProductName, 
         CurrentStock = s.TotalStocked - s.TotalSold, 
         s.CurrentSellingRate, 
         CashValue = (s.TotalStocked - s.TotalSold) * s.CurrentSellingRate, 
         s.LastStocked, 
         s.LastCostPrice, 
         s.LastQtyStocked 
        }).ToList(); 

File.AppendAllText(traceFile, CurrentStock.toTraceString()); 
return CurrentStock.ToList(); 

如何從以下行中的CurrentStock獲取toTraceString()?它沒有被解決如何從linq到實體查詢到toTraceString()?

File.AppendAllText(traceFile, CurrentStock.toTraceString()); 

回答

9

你不能因爲你已經呼籲ToList - 它不是一個LINQ到實體查詢,但只是一個List實例。順便說一句

var CurrentStock = (from s in DBViews.StockStatus 
        where s.ProductID != 10 
        orderby s.ProductName 
        select new 
        { 
         s.ProductID, 
         s.ProductName, 
         CurrentStock = s.TotalStocked - s.TotalSold, 
         s.CurrentSellingRate, 
         CashValue = (s.TotalStocked - s.TotalSold) * s.CurrentSellingRate, 
         s.LastStocked, 
         s.LastCostPrice, 
         s.LastQtyStocked 
        }); // No ToList here! 

File.AppendAllText(traceFile, ((ObjectQuery)CurrentStock).ToTraceString()); 
return CurrentStock.ToList(); 

試試這個。你爲什麼用dynamic而不是string?動態類型僅適用於有意義的特殊情況 - 情況並非如此。

+0

也感謝關於使用動態的建議。 – StackTrace 2012-07-24 07:59:21