c# – 如何从linq到实体查询获取toTraceString()?

前端之家收集整理的这篇文章主要介绍了c# – 如何从linq到实体查询获取toTraceString()?前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
@H_404_1@
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());

解决方法

你不能因为你已经调用了ToList – 它不是Linq-to-Entities查询而只是一个List实例.

试试这个:

var CurrentStock = (from s in DBViews.StockStatus
                    where s.ProductID != 10
                    orderby s.ProductName
                    select new
                    {
                        s.ProductID,s.LastQtyStocked
                    }); // No ToList here!

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

顺便说一句.为什么你使用动态而不是字符串?动态类型仅适用于有意义的特殊情况 – 事实并非如此.

猜你在找的C#相关文章