Giter Site home page Giter Site logo

dynamicexpressionbuilder's Introduction

Overview

Simple C# expression builder at run-time. Currently supports these operations:

public enum Operation
    {
        Equals,
        NotEquals,
        GreaterThan,
        LessThan,
        GreaterThanOrEqual,
        LessThanOrEqual,
        Contains,
        ContainsWithToLower,
        ToStringWithContains,
        StartsWith,
        EndsWith,
        StartsWithToLower,
        EndsWithToLower,
        StringEquals
    }

Implementation

Steps:

  • Build expression input list
public static IList<ExpressionInput> GetExpressionInputList()
        {
            return new List<ExpressionInput>
            {
                new ExpressionInput
                {
                    Operand = Operand.And, //First Item does not matter And or OR
                    Operation = Operation.Contains,
                    PropertyName = "Name",
                    Value = "Jack"
                },
                new ExpressionInput
                {
                    Operand = Operand.And,
                    Operation = Operation.Equals,
                    PropertyName = "Id",
                    Value = 1
                },
                new ExpressionInput
                {
                    Operand = Operand.And,
                    Operation = Operation.Equals,
                    PropertyName = "State",
                    Value = "FL"
                },
                new ExpressionInput
                {
                    Operand = Operand.Or,
                    Operation = Operation.Equals,
                    PropertyName = "CrimeRecord",
                    Value = false
                },
                new ExpressionInput
                {
                    Operand = Operand.And,
                    Operation = Operation.Equals,
                    PropertyName = "AnnualIncome",
                    Value = (long)50000 //Value need to be parsed to Expression's object (T) type. Here T is of Citizen type and AnnualIncome is of double type.
                }
            };
        }
  • Passed to ExpressionBuilder
            var expressionList = ExpressionInputGenerator.GetExpressionInputList();

            var expression = DynamicExpressionBuilder.ExpressionBuilder.GetExpression<Citizen>(expressionList); //Passing to expression builder. Here we are getting expression of Type Citizen
            Console.WriteLine($"Final Expression = {expression.ToString()}");

Output: Final Expression = t => ((((t.Name.Contains("Jack") AndAlso t.Id.Equals(1)) AndAlso t.State.Equals("FL")) Or t.CrimeRecord.Equals(False)) AndAlso t.AnnualIncome.Equals(50000))

  • Using Final expression
            var citizenRecords = CitizenRecordGenerator.GetCitizenRecordList();
            var expression = DynamicExpressionBuilder.ExpressionBuilder.GetExpression<Citizen>(expressionList)

            var filterCitizens = citizenRecords.Where(expression.Compile()); //Expression Implementation
            Console.WriteLine($"\nFilter records: {filterCitizens.Count()}");

Conclusion

Above code are from example console app which is inside project.

dynamicexpressionbuilder's People

Contributors

janaks09 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.