Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to get toTraceString() from linq to entities query?

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 can get toTraceString() from CurrentStock on in the following line? it ain't being resolved

File.AppendAllText(traceFile,  CurrentStock.toTraceString()); 
like image 999
StackTrace Avatar asked Jul 24 '12 07:07

StackTrace


1 Answers

You cannot because you have already called ToList - it is not a Linq-to-Entities query but simply a List instance.

Try this:

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 dynamic instead of string? Dynamic type is only for special cases where that makes sense - this is not the case.

like image 61
Ladislav Mrnka Avatar answered Sep 20 '22 22:09

Ladislav Mrnka