我试图使用Linq查询从MsSQL数据库选择不同的值,并将数据库字段作为条件传递,但是它给我错误。如果一个表具有以下数据:
以下是我的代码:
Name | Age | Class | school
----------------------------
Anna, 23, grade 2, Havard
Kendricks,34, grade 2, Havard
Vikander, 27, grade 3, Covenant
Hathaway, 18, grade1, Covenant
Gemma, 23, grade 4, Bowen
Jolie, 23, grade 5, Havard
Arteton, 24, grade 1, Bayero
Ana Armas 30, grade 2, Coventry
现在,我想通过传递“school”或“class”或更多字段来从上面的表中检索数据,然后根据这些字段返回不同的值。我该怎么做?
// filterParam - is the field(class, school)
// then how do I select the distinct values...
以下是我的代码:
public IEnumerable<ScbDataInfo> GetScbOptionsByFilter(string filterParam) {
using (SRSContext entityContext = new SRSContext()) {
var query = (from e in entityContext.Set<ScbDataInfo>()
where e[filterParam] == searchParam //i passed it here
orderby e.RefNo, e.datepmt
select e).Distinct();
return query.ToArray();
}
}