简体   繁体   中英

How to filter LINQ query on dynamic columns and dynamic property in C#?

I am facing trouble in fetching the filtered records in my LINQ Query to retrieve the columns dynamically based on condition and property also based on condition (ex. Contains, Equals, StartsWith, EndsWith).

I have a list of records like below -

 List<Employee> employees = new List<Employee>()
            {

                new Employee()
                {
                    name ="Andy", cityCriteria="Florida West", state   ="NYC"
                },
                new Employee()
                {
                    name = "John", cityCriteria = "West Virginia", state = "Arizona"
                },
                new Employee()
                {
                    name = "Nichole", cityCriteria = "East Florida", state = "NYC"
                }
            };

So, this is just some sample records the data will be coming from database and it will be so many records. Now, what i want to acheive is I have to notify all the persons if any Video posted with the City matching as per the list. So, i can receive NotificationValue as City:Florida:startsWith, City:Florida: Equals, City:Florida:Contains etc and there could be State Criteria too. So, how can i filter the records dynamically in the list like if input is Starts with i should use StartsWith ex

If Input is City:Florida:startsWith --> 
 var result = employees.where(i=>i.CityCriteria.StartsWith("Florida").toList();

If Input is City:Florida:Contains --> 
 var result = employees.where(i=>i.CityCriteria.Contains("Florida").toList();

If Input is City:Florida:EndsWith --> 
 var result = employees.where(i=>i.CityCriteria.EndsWith("Florida").toList();

If Input is City:Florida:Equals --> 
 var result = employees.where(i=>i.CityCriteria.Equals("Florida").toList();

I don't want to use multiple conditions and form the Where clause. I want it to be dynamic like if i receive starts with it should replace LINQ query starts, endswith,equals etc and also it should be flexible with dynamic column like I have to apply same logic for State,Country,Zip etc'

Please post some sample code if possible

I hope this helps you:

    private IEnumerable<Employee> FilterDynamically(IEnumerable<Employee> employees, string input, string cityName)
    {
        switch (input.ToLower())
        {
            case "starts with":
                return employees.Where(x => x.cityCriteria.StartsWith(cityName));

            case "ends with":
                return employees.Where(x => x.cityCriteria.EndsWith(cityName));

            case "contains":
                return employees.Where(x => x.cityCriteria.Contains(cityName));

            case "equals":
                return employees.Where(x => x.cityCriteria.Equals(cityName));
            
            default:
                 return Enumerable.Empty<Employee>();
        }
    }

There may be many solution to this, but usually like build an extensible one which are proved in my own projects.

//In case of addition filters u just need to update this class and everything will work else where
public class Filters
{        
    Filters() {
        maps.Add("startswith", StartsWith);
        maps.Add("Contains", Contains);
        maps.Add("Endswith", Endswith);
        maps.Add("Equals", Equals);
    }

    public static readonly Filters Instance = new Filters();

    public Func<Employee, string, bool> GetFilter(string filterClause)
        => maps.ContainsKey (filterClause) ? maps[filterClause] : None;

    Func<Employee, string, bool> StartsWith = (e, value) => e.cityCriteria.StartsWith(value);
    Func<Employee, string, bool> Contains = (e, value) => e.cityCriteria.Contains(value);
    Func<Employee, string, bool> Endswith = (e, value) => e.cityCriteria.EndsWith(value);
    Func<Employee, string, bool> Equals = (e, value) => e.cityCriteria.Equals(value);

    //In case none of the filter cluase do not match 
    Func<Employee, string, bool> None = (e, value) => true;

    //Filter clauses are made case insensitive by passing stringcomparer
    Dictionary<string, Func<Employee, string, bool>> maps =
        new Dictionary<string, Func<Employee, string, bool>>(StringComparer.OrdinalIgnoreCase);

}

An extension method to easy usage and consistency

public static class EmployeeExtensions
{
    public static IEnumerable<Employee> Filter(this IEnumerable<Employee> employees, string filterClause, string filterValue)
        => employees.Where(x => Filters.Instance.GetFilter(filterClause)(x, filterValue));
}

Usage as follows

public class Usage
{
    public void Test()
    {
        var filteredEmployees = 
            new Employee[0]
            .Filter("startswith", "florida")
            .ToList();
    }
}

A simple and fast solution to get property value will be to use DynamicMethod. Here is how I did it, and it is a working solution:

using Newtonsoft.Json;
using NUnit.Framework;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using System.Reflection.Emit;

namespace NUnitTestProject1
{
    public class Tests
    {
        static List<Employee> employees = new List<Employee>()
        {
            new Employee()
            {
                Name = "Andy", City = "Florida West", State = "NYC"
            },
            new Employee()
            {
                Name = "John", City = "West Virginia", State = "Arizona"
            },
            new Employee()
            {
                Name = "Nichole", City = "East Florida", State = "NYC"
            }
        };

        public enum Comparison
        {
            StartsWith,
            EndsWith,
            Equals
        }

        public struct Condition
        {
            public string PropertyName { get; set; }
            public string PropertyValue { get; set; }
            public Comparison Comparison { get; set; }
        }

        [TestCase("City", "Florida", Comparison.StartsWith, "Andy")]
        [TestCase("State", "Arizona", Comparison.Equals, "John")]
        public void TestConditions(string propertyName, string propertyValue, Comparison comparison, string expectedResult)
        {
            string jsonCondition = $"{{\"PropertyName\":\"{propertyName}\",\"PropertyValue\":\"{propertyValue}\",\"Comparison\":{(int)comparison}}}";
            Condition parsedCondition = JsonConvert.DeserializeObject<Condition>(jsonCondition);
            List<Employee> result = new List<Employee>();
            var getter = GetPropertGetter(typeof(Employee).ToString(), parsedCondition.PropertyName);
            switch (parsedCondition.Comparison)
            {
                case Comparison.StartsWith:
                    result = employees.Where(i => (getter(i) as string).StartsWith(parsedCondition.PropertyValue)).ToList();
                    break;
                case Comparison.EndsWith:
                    result = employees.Where(i => (getter(i) as string).EndsWith(parsedCondition.PropertyValue)).ToList();
                    break;
                case Comparison.Equals:
                    result = employees.Where(i => (getter(i) as string).Equals(parsedCondition.PropertyValue)).ToList();
                    break;
            }

            Assert.That(result.FirstOrDefault().Name, Does.Match(expectedResult));
        }

        Func<object, object> GetPropertGetter(string typeName, string propertyName)
        {
            Type t = Type.GetType(typeName);
            PropertyInfo pi = t.GetProperty(propertyName);
            MethodInfo getter = pi.GetGetMethod();

            DynamicMethod dm = new DynamicMethod("GetValue", typeof(object), new Type[] { typeof(object) }, typeof(object), true);
            ILGenerator lgen = dm.GetILGenerator();

            lgen.Emit(OpCodes.Ldarg_0);
            lgen.Emit(OpCodes.Call, getter);

            if (getter.ReturnType.GetTypeInfo().IsValueType)
            {
                lgen.Emit(OpCodes.Box, getter.ReturnType);
            }

            lgen.Emit(OpCodes.Ret);
            return dm.CreateDelegate(typeof(Func<object, object>)) as Func<object, object>;
        }
    }

    internal class Employee
    {
        private string name;
        private string city;
        private string state;

        public string Name { get => name; set => name = value; }
        public string City { get => city; set => city = value; }
        public string State { get => state; set => state = value; }
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM