【发布时间】:2018-02-11 11:46:27
【问题描述】:
如何将x.Demographic.AgeRange 替换为任何其他字段?
例如
var field_to_check = "Country";
x.Demographic.ReflectedProperty(field_to_check)=="USA"
var field_to_check = "AgeRnage";
x.Demographic.ReflectedProperty(field_to_check)=="20-30"
我尝试使用 rereflected 属性。但无法成功。
Favourability = db.Questions
.OrderByDescending(x => x.Responces.Count(y => y.Responseval == Constants.options.Agree || y.Responseval == Constants.options.Tend_to_Agree))
.Select(z => new
{
z.QuestionTitle,
Count = z.Responces.Where(x =>
x.Demographic.AgeRange == repval &&
(x.Responseval == Constants.options.Agree || x.Responseval == Constants.options.Tend_to_Agree)
)
.Count()
})
.Select(z => new
{
z.QuestionTitle,
z.Count,
Perc = ((z.Count / totresponcecount) * 100)
}
)
.ToList();
这样我就可以只编写一个 linq 语句作为动态过滤,而不是为所有必需属性编写 switch 语句。
【问题讨论】:
-
“与任何其他领域”是什么意思?你想达到什么目的?目前这感觉有点像 XY 问题。
-
@ Jon Skeet,类似 var feild = Country x.Demographic.feild == repval 而不是
-
抱歉,这仍然真的不清楚。请编辑您的问题以使其更清晰 - 并且最好也简化代码,因为您提供了相当多的代码,我怀疑其中大部分是无关紧要的。 minimal reproducible example 是理想的选择。
标签: c# .net linq expression-trees