0

I have the following requirement in my application.

My Entity Name is Payment. When making the Payment I have the following conditions. Based on these conditions I have to say it is payment or recovery or cash. I have defined all these conditions in a Dictionary.

 Dictionary<string, string> paymentTypes = new Dictionary<string, string>();
 paymentTypes.Add("Cash", "Payment.Type = 'C' and Recovery != ''");
 paymentTypes.Add("Recovery", "Payment.Recovery = ''");
 paymentTypes.Add("Payment", "Payment.Recovery != ''");

At run time how do I check the payment object values with dictionary? Based on the condition I have to pick the dictionary key.

I have tried several parses all of them or either mathematical parser they are not supporting string in the expressions.

I am not able to convert my Expressions in to dynamic expression. All these expressions will come from DB as string. How should I define the Expression when my Entity is generic? these are the expressions I made.

Expression<Func<Payment, bool>> cashFilter = payment => payment.Recovery == "" && payment.Type != "V" && payment.Type != "S"; 

            Expression<Func<Payment, bool>> paymentFilter = payment => payment.Recovery == "";

            Expression<Func<Payment, bool>> recoveryFilter = payment => payment.Recovery != ""; 
Suresh
  • 15
  • 1
  • 3
  • Are you attempting to use this to filter on Linq queries to a DB? If not exactly how do you plain to use them? – juharr Apr 02 '20 at 02:49
  • I have defined these rules (conditions) in a DB, I will get these conditions when my application loads. In this case I my current entity is payment, in some other case it can be Payroll or Employee. Based on the condition I should take next action. I am using this in Silverlight application with Entity framework. – Suresh Apr 02 '20 at 13:40

1 Answers1

0

I suggest using lambda instead of strings.

var paymentTypes = new Dictionary<string, Func<Payment,bool>>
{
    { "Cash", payment => payment.Type == 'C' && payment.Recovery != '' },
    { "Recovery", payment => payment.Recovery == '' },
    { "Payment", payment => payment.Type != 'C' && payment.Recovery != '' }
};

Then you can just evaluate it like this:

var paymentType = paymentTypes.Single( entry => entry.Value(payment) ).Key;
John Wu
  • 50,556
  • 8
  • 44
  • 80
  • Since the OP tagged EF I'd suggest using `Expression>` instead. – juharr Apr 02 '20 at 02:47
  • If I define as "Entity" instead of Payment object will it work so that it can be applied to different entities at runtime? I have to define this rules for my entire application. Based on the rules I should take some actions. Those rules are configurable from the DB based on the customer needs. – Suresh Apr 02 '20 at 13:46
  • As my entity is dynamic, Do I have construct expression like below in this example? https://stackoverflow.com/questions/8315819/expression-lambda-and-query-generation-at-runtime-simplest-where-example – Suresh Apr 02 '20 at 14:52
  • I am referring the below links as well. I should construct the Expression. I will have AND and OR conditions in my query. How should I make the generic expression and find out my action name from the dictionary?https://stackoverflow.com/questions/2486879/multiple-conditions-in-lambda-expressions-at-runtime-c-sharp https://stackoverflow.com/questions/31418069/creating-a-lambda-expression-at-runtime – Suresh Apr 02 '20 at 15:15
  • @Suresh If you want to apply this to different entities that have the same properties I'd suggest using an interface. – juharr Apr 02 '20 at 15:23
  • @juharr, Entity is the base class for all if these that is why I want to use "Entity". See this example. https://stackoverflow.com/questions/7801165/how-to-create-a-expression-lambda-when-a-type-is-not-known-until-runtime – Suresh Apr 02 '20 at 15:26
  • @Suresh Yeah, if you have a base class that defines these properties then that would work as well. – juharr Apr 02 '20 at 15:29