gpt4 book ai didi

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

转载 作者:太空狗 更新时间:2023-10-29 21:13:45 27 4
gpt4 key购买 nike

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.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?动态类型仅适用于有意义的特殊情况 - 事实并非如此。

关于c# - 如何从 linq 到实体查询中获取 toTraceString()?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11625774/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com