.NET使用表达式树来查看列表是否在另一个列表中

时间:2013-08-23 16:21:11

标签: c# lambda expression rules

我正在研究规则引擎。由于业务规则的性质,一种可能的解决方案是使用表达式根据规则动态调用方法。由于我是表达新手,我不知道如何做到以下几点:

public class Order
    {
        public Order()
        {
            this.Items = new List<Item>();
        }
        public int OrderId { get; set; }
        public Customer Customer { get; set; }
        public IList<Item> Items { get; set; }
    }

    public class Item
    {
        public decimal Cost { get; set; }
        public string ItemCode { get; set; }
        public int Status { get; set; }
    }

    public class Customer
    {
        public string FirstName { get; set; }
        public string LastName { get; set; }
        public Country Country { get; set; }
    }
    public class Country
    {
        public string CountryCode { get; set; }
    }
}

这是我使用以下命令填充Order对象的内容:

public Order GetOrder()
        {
            Order order = new Order()
            {
                OrderId = 1,
                Customer = new Customer()
                {
                    FirstName = "John",
                    LastName = "Doe",
                    Country = new Country()
                    {
                        CountryCode = "AUS"
                    }
                },
                Items = new List<Item>(){
                    new Item(){ ItemCode = "MM23", Cost=5.25M, Status=1},
                    new Item(){ ItemCode = "LD45", Cost=5.25M, Status=0},
                    new Item(){ ItemCode = "Test", Cost=3.33M, Status=1},
                }
            };
            return order;
        }

我的一条规则是检查订单中是否有商品代码(“MM23”,“LD45”)且状态不是1.

 GetOrder().Items.Where(x => list.Contains(x.ItemCode) && x.Status !=1).Any();

0 个答案:

没有答案