C# 要搜索的Linq查询
我有一个Employee类的集合,Employee类几乎没有属性,如部门、经理姓名、工资等级、职务等。现在在我的webapi中,我有一个搜索方法,在其中搜索webapi所有字段中的字符串 例如,如果我搜索Peter,它将搜索所有员工的所有字段(部门、经理姓名、工资等级、职务)。为此,我使用以下方法:-C# 要搜索的Linq查询,c#,asp.net,linq,entity-framework,asp.net-web-api,C#,Asp.net,Linq,Entity Framework,Asp.net Web Api,我有一个Employee类的集合,Employee类几乎没有属性,如部门、经理姓名、工资等级、职务等。现在在我的webapi中,我有一个搜索方法,在其中搜索webapi所有字段中的字符串 例如,如果我搜索Peter,它将搜索所有员工的所有字段(部门、经理姓名、工资等级、职务)。为此,我使用以下方法:- public IEnumerable<Employee> Search(string searchstr) { if (repository != nu
public IEnumerable<Employee> Search(string searchstr)
{
if (repository != null)
{
var query =
from employees in repository.GetEmployees()
where
(employees.departement != null && employees.departement.Contains(searchstr)) ||
(employees.payscale != null && employees.payscale.Contains(searchstr))
(movie.designation != null && movie.designation.Contains(searchstr)) )
select employees;
return query.AsEnumerable().OrderBy(c => c.employeeid);
}
else
{
throw new HttpResponseException(new HttpResponseMessage(HttpStatusCode.NotFound));
}
}
public IEnumerable搜索(string searchstr)
{
if(存储库!=null)
{
变量查询=
来自repository.GetEmployees()中的员工
哪里
(employees.department!=null&&employees.department.Contains(searchstr))||
(employees.payscale!=null&&employees.payscale.Contains(searchstr))
(movie.designation!=null&&movie.designation.Contains(searchstr)))
选择员工;
返回query.AsEnumerable().OrderBy(c=>c.employeeid);
}
其他的
{
抛出新的HttpResponseException(新的HttpResponseMessage(HttpStatusCode.NotFound));
}
}
虽然我得到了期望的结果,但我不必使用该查询。是否有其他方法重写相同的查询?即使工作正常,也感觉有点脏。我会考虑使用反射来获得类的属性,然后动态搜索它们。 这样做的好处是:如果你明天新增一处房产,你就不需要再做任何改变了 缺点是:可能没有那么好的表现,因为反射比简单地搜索你知道存在的东西要慢得多 话虽如此,我相信还有其他一些漂亮的高级linq技巧,也许其他人可以指出
看看上面的链接:)正如Noctis所说,使用反射会导致.NET运行时的繁重任务 下面是一些循环遍历类的所有属性并搜索字符串的示例代码。它使用反射;) 如果有任何问题,请留言 应用程序入口点中的代码:
[STAThread]
private static void Main(string[] args)
{
var person1 = new Person {Name = "The first name", Address = "The first address"};
var person2 = new Person {Name = "The second name", Address = "The second address"};
var results = SearchStringTroughAllProperties("second", new List<Person> {person1, person2});
}
以及SearchStringToughallProperties
方法:
private static IEnumerable<Person> SearchStringTroughAllProperties(string stringToSearch,
IEnumerable<Person> persons)
{
var properties =
typeof (Person).GetProperties()
.Where(x => x.CanRead && x.PropertyType == typeof (string))
.Select(x => x.GetMethod)
.Where(x => !x.IsStatic)
.ToList();
return persons.Where(person =>
properties.Select(property => (string) property.Invoke(person, null) ?? string.Empty)
.Any(propertyValueInInstance => propertyValueInInstance.Contains(stringToSearch)));
}
我觉得很好。感谢它的工作。唯一的问题是,很少有属性将int作为数据类型,将string[]作为数据类型。如何满足它?如何在不同数据类型(如int、string、string[])的字段中搜索searchstring您还想检查这些属性上的值是否一致,或者在检查值时忽略它们?刚刚看到您的新注释。如何将
int
转换为string
?我必须在集合的所有列中搜索字符串。其中一列的数据类型为string[]。现在如何处理此问题?我如何在上述代码中的string集合中搜索特定字符串?
private static IEnumerable<Person> SearchStringTroughAllProperties(string stringToSearch,
IEnumerable<Person> persons)
{
var properties =
typeof (Person).GetProperties()
.Where(x => x.CanRead && x.PropertyType == typeof (string))
.Select(x => x.GetMethod)
.Where(x => !x.IsStatic)
.ToList();
return persons.Where(person =>
properties.Select(property => (string) property.Invoke(person, null) ?? string.Empty)
.Any(propertyValueInInstance => propertyValueInInstance.Contains(stringToSearch)));
}
static IEnumerable<Person> SearchStringTroughAllProperties(string stringToSearch, IEnumerable<Person> persons)
{
var properties =
typeof (Person).GetProperties()
.Where(x => x.CanRead && (x.PropertyType == typeof (string) || x.PropertyType == typeof(string[])))
.Select(x => x.GetMethod)
.Where(x => !x.IsStatic)
.ToList();
foreach (var person in persons)
{
foreach (var property in properties)
{
bool yieldReturned = false;
switch (property.ReturnType.ToString())
{
case "System.String":
var propertyValueStr = (string) property.Invoke(person, null) ?? string.Empty;
if (propertyValueStr.Contains(stringToSearch))
{
yield return person;
yieldReturned = true;
}
break;
case "System.String[]":
var propertyValueStrArr = (string[]) property.Invoke(person, null);
if (propertyValueStrArr != null && propertyValueStrArr.Any(x => x.Contains(stringToSearch)))
{
yield return person;
yieldReturned = true;
}
break;
}
if (yieldReturned)
{
break;
}
}
}
}