C# 实体框架在数据库中按类型动态“包含”搜索字段
而不是以下代码…:C# 实体框架在数据库中按类型动态“包含”搜索字段,c#,entity-framework,entity-framework-4.1,C#,Entity Framework,Entity Framework 4.1,而不是以下代码…: public List<myUsers> SearchField(string search, string searchfield, int count = 15) { IQueryable<myUsers> DCs = this.entities.myUsers; if (!String.IsNullOrWhiteSpace(search)) { var DC2 = DCs; if (searc
public List<myUsers> SearchField(string search, string searchfield, int count = 15)
{
IQueryable<myUsers> DCs = this.entities.myUsers;
if (!String.IsNullOrWhiteSpace(search))
{
var DC2 = DCs;
if (searchfield == "userid")
{
DC2 = ListUtility.WhereInt(DCs, searchfield, search, 0);
// custom function to Contain-search an integer with Relfection
}
else if (searchfield == "username" || searchfield == "Lastname" || searchfield == "Firstname" || searchfield == "email")
{
DC2 = ListUtility.WhereStringContains(DC2, searchfield, search);
// custom function to Contain-search a string with REFLECTION
}
else if (searchfield == "date" || searchfield == "lastupdated")
{
// dates are impossible to work with--here's a function
// I don't know how to implement
DC2 = DateContainsSearch(DCs, searchfield, search);
}
return DC2.Take(count).ToList();
}
else
{
return new List<myUsers>();
}
}
我希望能够设计一个功能,看起来像下面的代码,基于T类型的实体。。。基于用户提交的预填充字符串字段searchText、searchField、searchType的SearchClass,我不知道我们是否需要最后一个:
public IQueryable<T> Search<T>(ref IQueryable<T> table) where T: class
{
foreach (SearchClass s in si)
{
table = table.WhereAnyType<T>(s.searchText, s.searchField, s.searchType); // all parameters are string
}
return table;
}
我在搜索类列表si中循环,因为可能存在组合搜索。。。例如,其中firstname.containsbob和username.containsBob99
换句话说,我希望我们能像对待字符串一样对待数据库中的每个字段
即使有反思,我也很难做出这样的事情。看看他的每个例子,他都在使用p.Description中的p.validf之类的东西。。。他已经知道这些字段并对它们进行硬编码。这一定是动态的,你说得对。我查找了前一段时间给出的答案,尝试以下操作:注意GetAccessExpression方法的属性参数是字符串。是的,这个反射似乎很接近,但我仍然需要为搜索字符串和搜索整数创建单独的函数。