当创建动态的LINQ实体框架排序和搜索顺序声明 [英] When creating dynamic linq sorting and searching order statements in Entity Framework

查看:162
本文介绍了当创建动态的LINQ实体框架排序和搜索顺序声明的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图建立一个函数动态生成基于一些参数不同的查询。我有点糊涂了LINQ语法和我不知道如果我这样做是正确的。

I'm trying to build a function to dynamically generate different queries based on some parameters. I am a bit confused on LINQ syntax and I'm not sure if I'm doing it right.

这组字符串类型参数被搜索(搜索文本框的值),searchfield(搜索的内容),limit_begin,limit_end有多少行和从哪里开始。 ORDER_BY字段通过向订购的。 order_sort进行排序的哪种方式。

The set of String type parameters are "search" (for search textbox value), "searchfield" (what to search), "limit_begin", "limit_end" for how many rows and where to start. "order_by" for which field to order by. "order_sort" for which way to sort.

我以前就发现这个计算器getpropertyvalue'反射功能,我希望它做什么,我根据我自己的跨pretation打算。

I found this 'getpropertyvalue' reflection function on stackoverflow before, I'm hoping it does what I'm intending based on my own interpretation.

 private static object GetPropertyValue(object obj, string property)
    {
        System.Reflection.PropertyInfo propertyInfo = obj.GetType().GetProperty(property);
        return propertyInfo.GetValue(obj, null);
    }


if (order_sort == "ASC")
        {
            (from a in entities.UserTable
             where GetPropertyValue(a, searchfield).ToString().Contains(search)
             select a)
                .OrderBy("a." + order_by)
                .Skip(Convert.ToInt32(limit_begin))
                .Take(Convert.ToInt32(limit_end))
                .ToList();
        }
        else if (order_sort == "DESC")
        {
            (from a in entities.UserTable
             where GetPropertyValue(a, searchfield).ToString().Contains(search)
             select a)
                .OrderByDescending("a." + order_by)
                .Skip(Convert.ToInt32(limit_begin))
                .Take(Convert.ToInt32(limit_end))
                .ToList();
        }

我的排序依据行收到错误,并VS2008凸显它的红说参数的类型不能从使用推断。

I'm getting an error on "Orderby" line, and VS2008 highlights it in red saying the type of argument cannot be inferred from the usage.

推荐答案

这是我如何在过去处理这个。通知是为单列的搜索和排序,它听起来就像你要的东西。

This is how I've dealt with this in the past. Notice is is a single column search and sort, which it sounds like what you are going for.

var users = entities.UserTable;

// Setup the default order column.
Func<SweetEntity, string> orderFunc = u => u.Field1;

switch (searchfield)
{
    case "Field1":
        orderFunc = u => u.Field1;
        users = users.Where(u => u.Field1.Contains(search));
        break;
    case "Field2":
        orderFunc = u => u.Field2;
        users = users.Where(u => u.Field2.Contains(search));
        break;
}

// If you need to get the total count, do it here:
var totalUserCount = users.Count();

// Apply sorting:
if (order_sort == "ASC")
{
    users = users.OrderBy(orderFunc);
}
else
{
    users = users.OrderByDescending(orderFunc);
}

// Apply paging:
users = users.Skip(Convert.ToInt32(limit_begin)).Take(Convert.ToInt32(limit_end));

我会做的比Convert.ToInt32,如int.TryParse其他的东西,但对于这个例子,我没有。

I would do something other than Convert.ToInt32, such as int.TryParse, but for the example I didn't.

编辑1:

如果你最终想要一个更强大的搜索,看看在LinqKit predicateBuilder(http://www.albahari.com/nutshell/$p$pdicatebuilder.aspx)。

If you end up wanting a more robust search, look into PredicateBuilder in LinqKit (http://www.albahari.com/nutshell/predicatebuilder.aspx).

编辑2:

我的例子只是不蜇包含在过滤部分。当然,你可以自定义用户请求的具体过滤所有的逻辑。如果他们在一个int过滤,你的过滤字符串转换为int,则可以只用在lambda前pression ==进行比较。是这样的:

My example just does sting contains in the filtering portion. Of course you could customize all that logic for the specific filter the user is requesting. If they're filtering on an int, you'd convert the filter string to an int, then can just compare with == in the lambda expression. Something like:

int myId;
if (int.TryParse(search, out myId))
{
    users = users.Where(u => u.SomeIntegerField == myId);
}

这篇关于当创建动态的LINQ实体框架排序和搜索顺序声明的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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