使用Serialize.Linq实现WCF方法参数可传入Linq

使用Nuge安装:Serialize.Linq



直接上代码:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Serialize.Linq;
using System.Linq.Expressions;
using Serialize.Linq.Serializers;
using Serialize.Linq.Nodes;

namespace ConsoleApplication35
{
    class Program
    {
        static void Main(string[] args)
        {

            List<Order> orderList = new List<Order>();
            for (int i = 0; i < 100; i++)
            {
                orderList.Add(new Order
                {
                    No = "NO" + i,
                    Price = i * 100
                });
            }

            var list = GetList<Order>(orderList, p => p.Price > 100 && p.No.Contains("99"));
            foreach (var item in list)
            {
                Console.WriteLine(item.No);

            }

            Console.ReadKey();

        }

        static List<T> GetList<T>(List<T> list, Expression<Func<T, bool>> express)
            where T : IOrder
        {
            ExpressionConverter expressionConvert = new ExpressionConverter();
            ExpressionNode expressionNode = expressionConvert.Convert(express);

            var express2 = expressionNode.ToExpression<Func<T, bool>>();

            if (list == null)
                return default(List<T>);
            return list.AsQueryable().Where<T>(express2).ToList();

        }
    }

    public interface IOrder { }
    public class Order : IOrder
    {
        public string No { get; set; }

        public decimal Price { get; set; }
    }
}

运行结果:


猜你喜欢

转载自blog.csdn.net/xiaoxionglove/article/details/72629537