NRules: match a collection

核能气质少年 提交于 2019-12-07 22:07:22

问题


I'm trying to figure out the BRE NRules and got some examples working but having a hard time to match a collection.

IEnumerable<Order> orders = null;

When()
    .Match<IEnumerable<Order>>(o => o.Where(c => c.Cancelled).Count() >= 3)
    .Collect<Order>(() => orders, o => o.Cancelled);

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

Basically what I want is if there are 3 orders cancelled then do some action. But I can't seem get a match on a collection, single variables do work.

The program:

var order3 = new Order(123458, customer, 2, 20.0);
var order4 = new Order(123459, customer, 1, 10.0);
var order5 = new Order(123460, customer, 1, 11.0);

order3.Cancelled = true;
order4.Cancelled = true;
order5.Cancelled = true;

session.Insert(order3);
session.Insert(order4);
session.Insert(order5);

session.Fire();

What am I doing wrong here?


回答1:


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));



回答2:


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.




回答3:


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



来源:https://stackoverflow.com/questions/28190119/nrules-match-a-collection

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