我正在创建一个复合匿名类型,并想知道我是否可以为NoPercent引用YesPercent字段?
var test = (from p in db.users
group p by p.ID into g
select new
{
ID = g.Key,
Frequency = g.Count(),
Question = g.FirstOrDefault().Question,
YesPercent = 50*564/32.5,
NoPercent = YesPercent - 10
})
答案 0 :(得分:9)
稍微改变一下
var test = (from p in db.users
group p by p.ID into g
let yesPercent = 50*564/32.5 // this variable will be available in your select
select new
{
ID = g.Key,
Frequency = g.Count(),
Question = g.FirstOrDefault().Question,
YesPercent = yesPercent,
NoPercent = yesPercent - 10
})
我认为你实际上发生了一些更复杂的事情。毕竟,YesPercent
的计算与查询数据无关,因此您可以很好地在查询之外声明一个变量并在其中使用它。