如何为CriteriaOperator过滤对象转换为lambda表达式,即:linq to xpo的动态where语句

Posted foreach

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了如何为CriteriaOperator过滤对象转换为lambda表达式,即:linq to xpo的动态where语句相关的知识,希望对你有一定的参考价值。

How to convert the CriteriaOperator to a lambda expression, so, the latter expression can be used in the IQueryable source

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using DevExpress.Xpo;
using DevExpress.Data.Filtering;
using System.Collections;
using DevExpress.XtraEditors;
using DevExpress.Data.Linq.Helpers;
using DevExpress.XtraEditors.Filtering;
using DevExpress.XtraEditors.Repository;
using DevExpress.Data.Filtering.Helpers;
using DevExpress.Data.Linq;

namespace DXSample
{
    public partial class Form1 : XtraForm
    {
        public Form1()
        {
            InitializeComponent();
        }

        IQueryable<Products> source;
        private void OnLoad(object sender, EventArgs e)
        {
            var products = new XPQuery<Products>(unitOfWork1);
            
            source = from product in products
                          where product.Quantity >= 20
                          select product;
           
            gridControl1.DataSource = source.ToList();
            CreateFilterColumns();
        }

        private void CreateFilterColumns()
        {
            filterControl1.FilterColumns.Add(new UnboundFilterColumn("ProductID", "ProductID", typeof(int),
                new RepositoryItemSpinEdit(), FilterColumnClauseClass.Generic));
            filterControl1.FilterColumns.Add(new UnboundFilterColumn("Quantity", "Quantity", typeof(int),
                new RepositoryItemSpinEdit(), FilterColumnClauseClass.Generic));
            filterControl1.FilterColumns.Add(new UnboundFilterColumn("Price", "Price", typeof(decimal),
               new RepositoryItemSpinEdit(), FilterColumnClauseClass.Generic));
            filterControl1.FilterColumns.Add(new UnboundFilterColumn("ProductName", "ProductName", typeof(string),
               new RepositoryItemTextEdit(), FilterColumnClauseClass.String));
            filterControl1.FilterColumns.Add(new UnboundFilterColumn("Discontinued", "Discontinued", typeof(bool),
               new RepositoryItemCheckEdit(), FilterColumnClauseClass.Generic));
        }

        private void OnUpdateFilter(object sender, EventArgs e)
        {
            gridControl1.BeginUpdate();
            try
            {
                CriteriaToExpressionConverter converter = new CriteriaToExpressionConverter();
                IQueryable<Products> filteredData = source.AppendWhere(converter, filterControl1.FilterCriteria) as IQueryable<Products>;
                gridControl1.DataSource = null;
                gridControl1.DataSource = filteredData.ToList();
            }
            finally
            {
                gridControl1.EndUpdate();
            }
        }

        private void OnSaveButtonClick(object sender, EventArgs e)
        {
            try
            {
                unitOfWork1.CommitChanges();
            }
            catch (Exception exc){
                MessageBox.Show(exc.ToString());
                unitOfWork1.ReloadChangedObjects();
            }
        }
    }
}
using System;
using DevExpress.Xpo;
namespace DXSample
{
    public class Products : XPLiteObject
    {
        int fProductID;
        [Key(true)]
        public int ProductID
        {
            get { return fProductID; }
            set { SetPropertyValue<int>("ProductID", ref fProductID, value); }
        }
      
        int fQuantity;
        public int Quantity
        {
            get { return fQuantity; }
            set { SetPropertyValue<int>("Quantity", ref fQuantity, value); }
        }

        decimal fPrice;
        public decimal Price
        {
            get { return fPrice; }
            set { SetPropertyValue<decimal>("Price", ref fPrice, value); }
        }

        string fProductName;
        public string ProductName
        {
            get { return fProductName; }
            set { SetPropertyValue<string>("ProductName", ref fProductName, value); }
        }

        bool fDiscontinued;

        public bool Discontinued
        {
            get { return fDiscontinued; }
            set { SetPropertyValue<bool>("Discontinued", ref fDiscontinued, value); }
        }

        public Products(Session session) : base(session) { }
        public Products() : base(Session.DefaultSession) { }
        public override void AfterConstruction() { base.AfterConstruction(); }
    }
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Windows.Forms;
using DevExpress.Xpo;
using DevExpress.Xpo.DB;
using DevExpress.Skins;

namespace DXSample
{
    static class Program
    {
        /// <summary>
        /// The main entry point for the application.
        /// </summary>
        [STAThread]
        static void Main()
        {
            string connectionString = AccessConnectionProvider.GetConnectionString("MyDB.mdb");
            XpoDefault.DataLayer = XpoDefault.GetDataLayer(connectionString, AutoCreateOption.DatabaseAndSchema);
            XpoDefault.Session = null;

            CreateData();

            SkinManager.EnableFormSkins();
            Application.EnableVisualStyles();
            Application.SetCompatibleTextRenderingDefault(false);
            Application.Run(new Form1());
        }

        static void CreateData()
        {
            using (UnitOfWork uow = new UnitOfWork())
            {
                if (uow.FindObject<Products>(null) == null)
                {
                    Products product = new Products(uow);
                    product.Discontinued = false;
                    product.Price = 100;
                    product.Quantity = 20;
                    product.ProductName = "Orange";

                    product = new Products(uow);
                    product.Discontinued = true;
                    product.Price = 140;
                    product.Quantity = 50;
                    product.ProductName = "Apple";

                    product = new Products(uow);
                    product.Discontinued = false;
                    product.Price = 30;
                    product.Quantity = 120;
                    product.ProductName = "Banana";

                    product = new Products(uow);
                    product.Discontinued = true;
                    product.Price = 200;
                    product.Quantity = 80;
                    product.ProductName = "Grape";

                    product = new Products(uow);
                    product.Discontinued = false;
                    product.Price = 300;
                    product.Quantity = 10;
                    product.ProductName = "Garnet";

                    product = new Products(uow);
                    product.Discontinued = true;
                    product.Price = 120;
                    product.Quantity = 320;
                    product.ProductName = "Mandarin";

                    product = new Products(uow);
                    product.Discontinued = false;
                    product.Price = 250;
                    product.Quantity = 60;
                    product.ProductName = "Kiwi";

                    uow.CommitChanges();
                }
            }
        }
    }
}

 

 

示例地址:https://www.devexpress.com/Support/Center/Example/Details/E2596

 

以上是关于如何为CriteriaOperator过滤对象转换为lambda表达式,即:linq to xpo的动态where语句的主要内容,如果未能解决你的问题,请参考以下文章

如何为 QTableWidget 创建过滤器?

使用 PyQT,如何为带有自定义列表的 QComboBox 过滤 mousePressEvent

如何为绘制的数据框设置时间过滤器 - Matplotlib

如何为three.js模型加载器将对象转换为json文件

如何为事务管理编写自定义过滤器

如何为一个特定的 servlet 应用过滤器?