在使用动态字段时,如果是对list进行linq查询,可以用反射来实现,如果是用Linq to Entity暂时没找到可以用反射的方法,但是可以用近似的SQL写。或者在逻辑层面解决问题。
public class PeopleCntorl
{
public Dictionary<string, IEnumerable<people>> Dic;
public PeopleCntorl()
{
List<people> peoples = new List<people>()
{
new people{ID=0, Name="Rose", Age=21 },
new people{ID=1, Name="Jack", Age=23 },
new people{ID=2,Name="Mike",Age=29}
};
Dic = new Dictionary<string, IEnumerable<people>>();
IEnumerable<people> people=from x in peoples
select x.Name;
IEnumerable<people> people2 = from x in peoples
select x.Age;
Dic.Add("1", people);
Dic.Add("2", people2);
}
public IEnumerable<people> GetEnumerable(string name)
{
IEnumerable<people> iEnumerale=Dic[name];
return iEnumerale;
}
}
此时如果要获取不同字段,只要修改Dictionary里面的表达式就行了。