我正在努力完成这样的事情,
var data = from p in db.Projects select new { Cost = p.CostReports.FirstOrDefault().Charged,Tax = Cost * 0.25 };
换句话说,我想使用Cost作为变量.可能吗?如果是这样,怎么办?
我的代码只是一个例子,我正在处理的项目有点复杂.
编辑:
我希望这是我正在努力做的更好的例子,
var data = (from p in db.Projects select new { Name = p.ProjectName,Customer = p.CustomerID,Cost = p.Cost }).GroupBy(p => p.Customer) .Select(g => new { Something = g.Where(p => p.Customer == g.Key).Sum(p => p.Cost),SomethingElse = Something * 0.25 });
解决方法
在查询中使用let关键字.
var data = from p in db.Projects let cost = p.CostReports.FirstOrDefault().Charged select new { Cost = cost,Tax = cost * 0.25 };
编辑
关于更新和禁止附加信息,我可能仍然会尝试通过重写查询结构来使用let.
var data = from g in db.Projects.Select(p => new { Name = p.ProjectName,Cost = p.Cost } ).GroupBy(p => p.Customer) let something = g.Sum(p => p.Cost) select new { Something = something,SomethingElse = something * 0.25 };