6
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() ?它没有得到解决

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

1 回答 1

10

你不能因为你已经调用ToList了 - 它不是一个 Linq-to-Entities 查询,而只是一个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?动态类型仅适用于有意义的特殊情况 - 情况并非如此。

于 2012-07-24T07:37:14.050 回答