Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/284.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/linq/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/user-interface/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 编写此linq查询Pt2的更好方法_C#_Linq_Entity Framework - Fatal编程技术网

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);
    }