构建动态LINQ查询的最佳方式 [英] The best way to build Dynamic LINQ query

查看:173
本文介绍了构建动态LINQ查询的最佳方式的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

您好我正在寻找最好的方法编写动态LINQ查询。

Hi I am looking for best method for writing Dynamic LINQ query.

我有一个像

public IQueryable<Student> FindByAllStudents(int? id, string Name, int? CourseID, bool? IsActive) // like this way, all field values are passed
    {    
        // code for compairision
        return db.Student;
    }

我们也可以写db.Students.where(predicate)

we can also write db.Students.where(predicate)

var students = from s in db.students where s.Name.Contains(Name)
				s.ID.Equals(id)
				//and so on....

所以将这种方法,如果我不通过ID(即空)的作品?
是所有数据类型正确方法?

So will this method works if i don't pass ID (i.e. Null)? is proper way for all the datatypes?

问题的关键是功能可以将所有空值作为从语句SELECT *等价的参数。

The point is function can have all null values as a parameter for equivalence of select * from statement.

任何一个可以帮助我建立样本code最好的查询?

can any one help me to build best query with sample code?

推荐答案

好吧,它不是完全清楚你想要什么,但如果你想只添加其中,对于那些非空参数的条款,你可以做

Okay, it's not entirely clear what you want, but if you're trying to only add where clauses for the parameters which are non-null, you could do:

public IQueryable<Student> FindByAllStudents
    (int? id, string name, int? courseID, bool? isActive)
{    
    IQueryable<Student> query = db.Student;
    if (id != null)
    {
        query = query.Where(student => student.ID == id.Value);
    }
    if (name != null)
    {
        query = query.Where(student => student.Name.Contains(name));
    }
    if (courseID != null)
    {
        query = query.Where(student => student.CourseID == courseID.Value);
    }
    if (isActive != null)
    {
        query = query.Where(student => student.IsActive == isActive.Value);
    }
    return query;
}

我还没有试过,它的的可能的是LINQ to SQL的将由code感到困惑找到空值类型的值。您可能需要写code是这样的:

I haven't tried that, and it's possible that LINQ to SQL would get confused by the code to find the value of the nullable value types. You may need to write code like this:

    if (courseID != null)
    {
        int queryCourseID = courseID.Value;
        query = query.Where(student => student.CourseID == queryCourseID);
    }

这是值得尝试的简单形式,虽然第一:)

It's worth trying the simpler form first though :)

当然,这一切都变得有点恼火。一个有用的扩展方法可以让生活更简洁:

Of course, all this gets a bit irritating. A helpful extension method could make life more concise:

public static IQueryable<TSource> OptionalWhere<TSource, TParameter>
    (IQueryable<TSource> source,
     TParameter? parameter, 
     Func<TParameter, Expression<Func<TSource,bool>>> whereClause)
    where TParameter : struct
{
    IQueryable<TSource> ret = source;
    if (parameter != null)
    {
        ret = ret.Where(whereClause(parameter.Value));
    }
    return ret;
}

然后,您会使用这样的:

You'd then use it like this:

public IQueryable<Student> FindByAllStudents
    (int? id, string name, int? courseID, bool? isActive)
{    
    IQueryable<Student> query = db.Student
        .OptionalWhere(id, x => (student => student.ID == x))
        .OptionalWhere(courseID, x => (student => student.CourseID == x))
        .OptionalWhere(isActive, x => (student => student.IsActive == x));
    if (name != null)
    {
        query = query.Where(student => student.Name.Contains(name));
    }
    return query;
}

使用高阶函数这样可以得到,如果你不与它真的很舒服的,所以如果你没有做很多的查询这样你可能要坚持使用时间较长,但更简单的code混乱

Using a higher order function like this could get confusing if you're not really comfortable with it though, so if you're not doing very many queries like this you might want to stick with the longer but simpler code.

这篇关于构建动态LINQ查询的最佳方式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆