我正在努力完成这样的事情,
var data = from p in db.Projects
select new
{
Cost = p.CostReports.FirstOrDefault().Charged,
Tax = Cost * 0.25
};
Run Code Online (Sandbox Code Playgroud)
换句话说,我想使用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
});
Run Code Online (Sandbox Code Playgroud)
Ant*_*ram 25
let在查询中使用关键字.
var data = from p in db.Projects
let cost = p.CostReports.FirstOrDefault().Charged
select new
{
Cost = cost,
Tax = cost * 0.25
};
Run Code Online (Sandbox Code Playgroud)
编辑
关于您的更新和禁止其他信息,我可能仍然倾向于let通过重写您的查询结构来使用.
var data = from g in db.Projects.Select(p =>
new
{
Name = p.ProjectName,
Customer = p.CustomerID,
Cost = p.Cost
}
).GroupBy(p => p.Customer)
let something = g.Sum(p => p.Cost)
select new
{
Something = something,
SomethingElse = something * 0.25
};
Run Code Online (Sandbox Code Playgroud)
在这种情况下,from g in ...指的是分组数据,它允许您对此数据使用查询表达式语法,包括let.
var data = from p in db.Projects
let cost = p.CostReports.FirstOrDefault().Charged
select new
{
Cost = cost,
Tax = cost * 0.25
};
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
2069 次 |
| 最近记录: |