C# 编写此linq查询Pt2的更好方法
针对这一问题: 如何在该线程中按照相同的模式构建动态查询 例如,方法的签名更改为:C# 编写此linq查询Pt2的更好方法,c#,linq,entity-framework,C#,Linq,Entity Framework,针对这一问题: 如何在该线程中按照相同的模式构建动态查询 例如,方法的签名更改为: public List<PeopleSearchList> GetPeople(string filter, string searchType, string searchOption) { return a new List of type PeopleSearchList } 我算出来了 我只是想把解决方案贴出来让别人看 public List
public List<PeopleSearchList> GetPeople(string filter, string searchType, string searchOption)
{
return a new List of type PeopleSearchList
}
我算出来了
我只是想把解决方案贴出来让别人看
public List<PeopleSearchList> GetPeople(string filter, string searchType, string searchOption)
{
IQueryable<Person> query = _context.People;
PropertyInfo property = typeof (Person).GetProperty(searchType);
MethodInfo method = typeof (string).GetMethod(searchOption, new[] {typeof (string)});
query = query.Where(WhereExpression(property, method, filter));
IQueryable<PeopleSearchList> resultQuery = query.Select(p => new PeopleSearchList
{
Firstname = p.Firstname,
Group = p.Level.Year,
IdentityCode = p.IdentityCode,
LoanCount = p.Loans.Count(),
Surname = p.Surname
}
).OrderBy(p => p.Surname);
return resultQuery.ToList();
}
Expression<Func<Person, bool>> WhereExpression(PropertyInfo property, MethodInfo method, string filter)
{
var param = Expression.Parameter(typeof(Person), "o");
var propExpr = Expression.Property(param, property);
var methodExpr = Expression.Call(propExpr, method, Expression.Constant(filter));
return Expression.Lambda<Func<Person, bool>>(methodExpr, param);
}
public List GetPeople(字符串过滤器、字符串搜索类型、字符串搜索选项)
{
IQueryable query=\u context.People;
PropertyInfo property=typeof(Person).GetProperty(searchType);
MethodInfo method=typeof(string).GetMethod(searchOption,new[]{typeof(string)});
query=query.Where(WhereExpression(属性、方法、过滤器));
IQueryable resultQuery=query.Select(p=>newpeopleSearchList
{
Firstname=p.Firstname,
组=p.Level.Year,
IdentityCode=p.IdentityCode,
LoanCount=p.Loans.Count(),
姓氏
}
).OrderBy(p=>p.姓氏);
返回resultQuery.ToList();
}
表达式WhereExpression(PropertyInfo属性、MethodInfo方法、字符串筛选器)
{
var param=表达式参数(typeof(Person),“o”);
var propExpr=Expression.Property(param,Property);
var methodExpr=Expression.Call(propExpr,方法,Expression.Constant(filter));
返回表达式.Lambda(methodExpr,param);
}
您可以使用
public List<PeopleSearchList> GetPeople(string filter, string searchType, string searchOption)
{
IQueryable<Person> query = _context.People;
PropertyInfo property = typeof (Person).GetProperty(searchType);
MethodInfo method = typeof (string).GetMethod(searchOption, new[] {typeof (string)});
query = query.Where(WhereExpression(property, method, filter));
IQueryable<PeopleSearchList> resultQuery = query.Select(p => new PeopleSearchList
{
Firstname = p.Firstname,
Group = p.Level.Year,
IdentityCode = p.IdentityCode,
LoanCount = p.Loans.Count(),
Surname = p.Surname
}
).OrderBy(p => p.Surname);
return resultQuery.ToList();
}
Expression<Func<Person, bool>> WhereExpression(PropertyInfo property, MethodInfo method, string filter)
{
var param = Expression.Parameter(typeof(Person), "o");
var propExpr = Expression.Property(param, property);
var methodExpr = Expression.Call(propExpr, method, Expression.Constant(filter));
return Expression.Lambda<Func<Person, bool>>(methodExpr, param);
}