基本信息
源码名称:动态Linq 查询条件(PredicateExtensions) 示例源码下载
源码大小:8.79M
文件格式:.rar
开发语言:C#
更新时间:2014-06-14
   友情提示:(无需注册或充值,赞助后即可获取资源下载链接)

     嘿,亲!知识可是无价之宝呢,但咱这精心整理的资料也耗费了不少心血呀。小小地破费一下,绝对物超所值哦!如有下载和支付问题,请联系我们QQ(微信同号):813200300

本次赞助数额为: 2 元 
   源码介绍

页面代码:


using System;
using System.Web.UI;
using System.Linq.Expressions;

public partial class _Default : System.Web.UI.Page
{
    private OrderController controller = new OrderController();
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            Bindgrid(null,null);
        }

    }

    public void Bindgrid(Expression<Func<Orders, bool>> whereqQuery ,QueryableOrderEntry<Orders, string> order)
    {
        grid.DataSource = controller.GetOrders(grid.CurrentPageIndex, grid.PageSize, whereqQuery, order);// 分页数目应该用分页控件,这里只是简单测试,所以偷个懒
        grid.DataBind();
    }

    protected void btnSearch_Click(object sender, ImageClickEventArgs e)
    {
       
        Bindgrid(GetWhere(), null);
        
    }

    public Expression<Func<Orders, bool>> GetWhere()
    {
        Expression<Func<Orders, bool>> expression = PredicateExtensions.True<Orders>();
        if (!string.IsNullOrEmpty(txtCustomerId.Text.Trim()))
        {
            string str = txtCustomerId.Text.Trim();
            expression = expression.And(o => o.CustomerID.Contains(str));
        }
        if (!string.IsNullOrEmpty(txtEmplyeeId.Text.Trim()))
        {
            string str = txtEmplyeeId.Text.Trim();
            expression = expression.And(o => o.EmployeeID.HasValue && o.EmployeeID.Value.Equals(int.Parse(str)));
        }
        if (txtOrderDateStart.SelectedDate.HasValue)
        {
            DateTime dt = txtOrderDateStart.SelectedDate.Value;
            expression = expression.And(o => o.OrderDate.HasValue && o.OrderDate.Value >= dt);
        }

        if (txtOrderDateEnd.SelectedDate.HasValue)
        {
            DateTime dt = txtOrderDateEnd.SelectedDate.Value;
            expression = expression.And(o => o.OrderDate.HasValue && o.OrderDate.Value <= dt);
        }
        return expression;
    }
    protected void grid_SortCommand(object source, Telerik.Web.UI.GridSortCommandEventArgs e)
    {
        if (e.NewSortOrder == Telerik.Web.UI.GridSortOrder.Ascending)
        {
           
        }
    }
    protected void grid_PageIndexChanged(object source, Telerik.Web.UI.GridPageChangedEventArgs e)
    {
        Bindgrid(GetWhere(), null);
    }
}


PredicateExtensions类代码:


using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Linq.Expressions;


public static class PredicateExtensions
{
    public static Expression<Func<T, bool>> True<T>() { return f => true; }

    public static Expression<Func<T, bool>> False<T>() { return f => false; }

    public static Expression<Func<T, bool>> Or<T>(this Expression<Func<T, bool>> expression1, Expression<Func<T, bool>> expression2)
    {
        var invokedExpression = Expression.Invoke(expression2, expression1.Parameters.Cast<Expression>());

        return Expression.Lambda<Func<T, bool>>(Expression.Or(expression1.Body, invokedExpression), expression1.Parameters);
    }

    public static Expression<Func<T, bool>> And<T>(this Expression<Func<T, bool>> expression1, Expression<Func<T, bool>> expression2)
    {
        var invokedExpression = Expression.Invoke(expression2, expression1.Parameters.Cast<Expression>());

        return Expression.Lambda<Func<T, bool>>(Expression.And(expression1.Body, invokedExpression), expression1.Parameters);
    }
}