C#中的LINQ根据属性名where子句中 [英] C# Linq where clause according to property name
问题描述
让我们说我有下面的类:
公共类Person { 公共字符串名字{获得;组; }
公共字符串姓{搞定;组; }
公众诠释年龄{搞定;组; }
公共字符串性别{搞定;组; }}
另外,我有以下的方法,我通过一个存储库接触到的人的数据。
公开的IEnumerable<&人GT; getPeople(字符串searchField,串SEARCHTERM){ // _ repo.GetAll()返回的IEnumerable<&人GT;
VAR模型= _repo.GetAll(); //这里需要逻辑过滤 回归模型;
}
正如你可以看到我获得两个参数的方法: searchField
和 SEARCHTERM
。
searchField
是其价值将被用于过滤的字段名称。 SEARCHTERM
是将用于与retrived值的(对不起,如果我不清楚这里,但这是我能拿出最)比较值的EM>
我通常会做的是如下:
公开的IEnumerable<&人GT; getPeople(字符串searchField,串SEARCHTERM){ // _ repo.GetAll()返回的IEnumerable<&人GT;
VAR模型= _repo.GetAll(); 开关(searchField){ 案名字:
模型= model.Where(X => x.FirstName == SEARCHTERM);
打破; 案姓:
模型= model.Where(X => x.SurName == SEARCHTERM);
打破; //不断去
} 回归模型;}
这会工作得很好。但是,如果我做我的类的变化,这code将有一个变化缺乏一些功能,打破或者,如果我增加新的属性这个类。
我所寻找的是类似如下:
注意:
这低于code完全属于我的想象力,有没有这样的一个
东西存在。
块引用>模型= model.Where(X => x.GetPropertyByName(searchField)== SEARCHTERM);
我是飞得太高这里如果这是不可能或成为白痴,如果已经有一个内置在这种方式?
解决方案看起来像你需要的动态LINQ查询
<一个href=\"http://weblogs.asp.net/scottgu/archive/2008/01/07/dynamic-linq-part-1-using-the-linq-dynamic-query-library.aspx\" rel=\"nofollow\">http://weblogs.asp.net/scottgu/archive/2008/01/07/dynamic-linq-part-1-using-the-linq-dynamic-query-library.aspx
Let's say I have the following class :
public class Person { public string FirstName { get; set; } public string SurName { get; set; } public int Age { get; set; } public string Gender { get; set; } }
Also, I have the following method and I am reaching out to person data via a repository.
public IEnumerable<Person> getPeople(string searchField, string searchTerm) { //_repo.GetAll() returns IEnumerable<Person> var model = _repo.GetAll(); //Need the logic here for filtering return model; }
As you can see I am getting two parameter for the method :
searchField
andsearchTerm
.
searchField
is for the field name whose value will be used for filtering.searchTerm
is the value which will be used to compare with retrived value (sorry if I am not clear here but this is the most I can come up with)What I would normally do is as follows :
public IEnumerable<Person> getPeople(string searchField, string searchTerm) { //_repo.GetAll() returns IEnumerable<Person> var model = _repo.GetAll(); switch(searchField) { case "FirstName": model = model.Where(x => x.FirstName == searchTerm); break; case "SurName": model = model.Where(x => x.SurName == searchTerm); break; //Keeps going } return model; }
Which will work just fine. But if I make a change on my class, this code will have a change to break or be in lack of some functions if I add new properties this class.
What I am looking for is something like below :
NOTE :
This below code completely belongs to my imagination and there is no such a thing exists.
model = model.Where(x => x.GetPropertyByName(searchField) == searchTerm);
Am I flying too high here if it is impossible or being complete idiot if there is already a built in way for this?
解决方案Looks like you need Dynamic Linq queries:
这篇关于C#中的LINQ根据属性名where子句中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!