You can create a generic search method where the key is unknown

You can create a general search method where the key is unknown; for example, the key for the list will be passed to the parameter, and it will perform a similar search and return the filtered list.

The code should look something like this:

public List<T> LikeSearch<T>(List<T> AllData,T key, string searchString)
{
  List<T> _list = new List<T>();
  //Perform the search on AllData based on searchString passed on the key   
  //given
 return _list;
}

Usage will look like this:

Example 1

List<Users> _users = LikeSearch<Users>(AllUsers,'Name','sam');

Where AllUsersis a list of 100 users.

Example 2

List<Customers> _cust = LikeSearch<Customers>(AllCustomers,'City','London');

Where AllCustomersis a list of 100 Customers.

I apologize

+4
source share
3 answers

Assuming you keyalways refer to a public property implemented by any type T, you can do the following:

public static List<T> LikeSearch<T>(this List<T> data, string key, string searchString)
{
    var property = typeof(T).GetProperty(key, BindingFlags.Public | BindingFlags.GetProperty | BindingFlags.Instance);

    if (property == null)
        throw new ArgumentException($"'{typeof(T).Name}' does not implement a public get property named '{key}'.");

    //Equals
    return data.Where(d => property.GetValue(d).Equals(searchString)).ToList();

    //Contains:
    return data.Where(d => ((string)property.GetValue(d)).Contains(searchString)).ToList();
}
+2
source

, ... , . ...

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text.RegularExpressions;
using System.Linq.Expressions;
using System.Reflection;
namespace Rextester
{
    public class Program
    {
        public static void Main(string[] args)
        {

            List<Demo> all= new List<Demo>();
            all.Add(new Demo{Name="a"});
            all.Add(new Demo{Name="ab"});
            all.Add(new Demo{Name="abc"});
            all.Add(new Demo{Name="cba"});
            all.Add(new Demo{Name="bac"});
            all.Add(new Demo{Name="ddd"});

            var t= Filter(all,"Name","a");

            Console.WriteLine(t.Count);
        }

        public static List<T> Filter<T>(List<T> Filterable, string PropertyName, object ParameterValue)
        {
          ConstantExpression c = Expression.Constant(ParameterValue);
          ParameterExpression p = Expression.Parameter(typeof(T), "xx");
          MemberExpression m = Expression.PropertyOrField(p, PropertyName);

          MethodInfo method = typeof(string).GetMethod("Contains", new[] { typeof(string) });  

          var containsMethodExp = Expression.Call(m, method, c);
          var Lambda= Expression.Lambda<Func<T, bool>>(containsMethodExp, p);           

          //var Lambda = Expression.Lambda<Func<T, Boolean>>(Expression.Equal(c, m), new[] { p });

          Func<T, Boolean> func = Lambda.Compile();
          return Filterable.Where(func).ToList();
        }
    }

    public class Demo
    {
        public string Name{get;set;}
    }
}
+3

linq Where

list.Where(x => x.YourKey.Contains(searchString))

1

List<Users> _users = AllUsers.Where(x => x.Name.Contains("sam"));

2

List<Customers> _cust = AllCustomers.Where(x => x.City.Contains("London"));

:

public List<T> LikeSearch<T>(List<T> list, Func<T, string> getKey, string searchString)
{
    return list.Where(x => getKey(x).Contains(searchString)).ToList();
}

:

1

List<Users> _users = LikeSearch(AllUsers, x => x.Name, "sam");

2

List<Customers> _cust = LikeSearch(AllCustomers, x => x.City, "London");

:

Contains .

( ...):

InBetween OneProperty: 00: 00: 00.0026050

Moumit OneProperty: 00: 00: 00.0013360

Mine OneProperty: 00: 00: 00.0010390

, , -

InBetween LotProperties: 00: 00: 00.0026378

Moumit LotProperties: 00: 00: 00.0012155

Mine LotProperties: 00: 00: 00.0010021

, Moumit , , . , , , GetProperty GetValue .

:

    static void Main(string[] args)
    {
        int size = 10000;
        Dictionary<string, List<long>> time = new Dictionary<string, List<long>>()
        {
            {"InBetween OneProperty", new List<long>() },
            {"Moumit OneProperty", new List<long>() },
            {"Mine OneProperty", new List<long>() },
            {"InBetween LotProperties", new List<long>() },
            {"Moumit LotProperties", new List<long>() },
            {"Mine LotProperties", new List<long>() },
        };
        List<OneProperty> oneProperties = new List<OneProperty>();
        List<LotProperties> lotProperties = new List<LotProperties>();
        for (int i = 0; i < size; ++i)
        {
            oneProperties.Add(new OneProperty() { Key = i.ToString() });
            lotProperties.Add(new LotProperties() { Key = i.ToString() });
        }
        Stopwatch sw = new Stopwatch();
        for (int i = 0; i < 1000; ++i)
        {
            sw.Start();
            InBetween.LikeSearch(oneProperties, "Key", "999");
            sw.Stop();
            time["InBetween OneProperty"].Add(sw.Elapsed.Ticks);
            sw.Reset();
            sw.Start();
            Moumit.Filter(oneProperties, "Key", "999");
            sw.Stop();
            time["Moumit OneProperty"].Add(sw.Elapsed.Ticks);
            sw.Reset();
            sw.Start();
            Mine.LikeSearch(oneProperties, x => x.Key, "999");
            sw.Stop();
            time["Mine OneProperty"].Add(sw.Elapsed.Ticks);
            sw.Reset();

            sw.Start();
            InBetween.LikeSearch(lotProperties, "Key", "999");
            sw.Stop();
            time["InBetween LotProperties"].Add(sw.Elapsed.Ticks);
            sw.Reset();
            sw.Start();
            Moumit.Filter(lotProperties, "Key", "999");
            sw.Stop();
            time["Moumit LotProperties"].Add(sw.Elapsed.Ticks);
            sw.Reset();
            sw.Start();
            Mine.LikeSearch(lotProperties, x => x.Key, "999");
            sw.Stop();
            time["Mine LotProperties"].Add(sw.Elapsed.Ticks);
            sw.Reset();
        }
        foreach (string key in time.Keys)
            Console.WriteLine($"{key}: {new TimeSpan((long)time[key].Average())}");
        Console.ReadKey();
    }

    class OneProperty
    {
        public string Key { get; set; }
    }
    class LotProperties
    {
        public string A { get; set; }
        public string B { get; set; }
        public string C { get; set; }
        public string D { get; set; }
        public string E { get; set; }
        public string F { get; set; }
        public string G { get; set; }
        public string H { get; set; }
        public string I { get; set; }
        public string J { get; set; }
        public string K { get; set; }
        public string L { get; set; }
        public string M { get; set; }
        public string N { get; set; }
        public string O { get; set; }
        public string P { get; set; }
        public string Q { get; set; }
        public string R { get; set; }
        public string S { get; set; }
        public string T { get; set; }
        public string U { get; set; }
        public string V { get; set; }
        public string W { get; set; }
        public string X { get; set; }
        public string Y { get; set; }
        public string Z { get; set; }
        public string Key { get; set; }
    }
0
source

Source: https://habr.com/ru/post/1652319/


All Articles