NRules: match a collection

坚强是说给别人听的谎言 提交于 2019-12-06 05:49:02

With the 0.3.1 version of NRules, the following will activate the rule when you collected 3 or more canceled orders:

IEnumerable<Order> orders = null;

When()
    .Collect<Order>(() => orders, o => o.Cancelled)
        .Where(x => x.Count() >= 3);
Then()
    .Do(ctx => orders.ToList().ForEach(o => o.DoSomething()));

Update:

For posterity, starting with version 0.4.x the right syntax is to use reactive LINQ. Matching a collection will look like this:

IEnumerable<Order> orders = null;
When()
    .Query(() => orders, q => q
        .Match<Order>(o => o.Cancelled)
        .Collect()
        .Where(x => x.Count() >= 3));
Then()
    .Do(ctx => DoSomething(orders));

In your example, it should be pretty straightforward

IEnumerable<Order> orders = null;

When()
    .Collect<Order>(() => orders, o => o.Cancelled == true);

Then()
    .Do(ctx => orders.ToList().ForEach(o => o.DoSomething()));

I think the important part is the o.Cancelled alone without the == true. I know this sound wack, but somehow the property evaluation alone that is not an expression (i.e. just the property) is not well supported in NRules.

I ran into this problem and when I added the == true everything fell into place.

How to join Multiple Collection based on some expression like

  IEnumerable<RawMsp> rawMsps = null;
        IEnumerable<AsmMasterView> asmMasterViews = null;
        IEnumerable<AsmInvestor> asmInvestors = null;            

        When()
            .Match<AsmInvestor>(() => rawMsps)
            .Match<AsmInvestor>(() => asmInvestor, i => i.InvestorId.ToString() == rawMsp.INVESTOR_CODE)
            .Match<AsmMasterView>(() => asmMasterView, x => x.ChildAssumptionHistId == asmInvestor.AssumptionHistId);    

Match is applicable individual object , Not sure apply equals of Enumerable Objects

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!