How to get toTraceString () from linq request to objects?

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();

How to get toTraceString () from CurrentStock in the next line? he does not dare

File.AppendAllText(traceFile,  CurrentStock.toTraceString()); 
+5
source share
1 answer

You cannot, because you have already called ToList- this is not a Linq-to-Entities request, but just an instance List.

Try the following:

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();

Btw. why are you using dynamicinstead string? The dynamic type is intended only for special cases when it makes sense - it is not.

+9
source

All Articles