0

LINQ Expression を拡張して、任意のプロパティ名と値を引数として使用して結果を取得するこのプログラムがあります。私の拡張機能は 1 つのプロパティに対してのみ正常に動作しますが、検索して 2 つ以上のプロパティをフィルターとして渡す必要があります。LINQ の拡張を手伝ってくれる人はいますか? 以下は私のコードの実装です。コンソール アプリケーションを使用しました。

呼び出したばかりのユーザーのリストのサンプル コードに基づいて

GetByPropertyName("PropertyName", "Value");

例えば

var user = GetByPropertyName("FirstName", "James");

Program.cs

using System;
using System.Collections.Generic;
using System.Linq;
using System.Linq.Expressions;
using System.Text;
using System.Threading.Tasks;

namespace ConsoleApplication1
{
    class Program
    {
        static List<User> _users = new List<User>();
        static void Main(string[] args)
        {
            var users = new List<User> { 
                new User { FirstName = "John", MiddleName = "Hall", LastName = "Long", Email="john.long@gmail.com"}, 
                new User { FirstName = "John", MiddleName = "Wine", LastName = "Crawford", Email="john.crawford@gmail.com" }, 
                new User { FirstName = "James", MiddleName = "Cage", LastName = "Hall", Email="james.hall@hotmail.com" }, 
                new User { FirstName = "Larry", MiddleName = "Wine", LastName = "Crawford", Email="larry.crawford@gmail.com" },
                new User { FirstName = "Jennifer", MiddleName = "Wine", LastName = "Long", Email="jennifer.long@gmail.com"} 
            };
            //works okay for one property name
            _users = users;
            var user = GetByPropertyName("FirstName", "James");

            //works okay for one property name
            _users = users;
            var user1 = GetByPropertyName("Email", "james.hall@hotmail.com");

            //NEED HELP
            //For GetByPropertyNames two or more properties
            var filters = new Dictionary<object, object>();
            filters.Add("FirstName", "John");
            filters.Add("Email", "john.long@gmail.com");
            var user2 = GetByPropertyNames(filters);
        }
        public static User GetByPropertyName(object propertyName, object value)
        {
            var result = _users.AsQueryable().Where(propertyName.ToString(), value).FirstOrDefault();
            return result;
        }

        public static User GetByPropertyNames(Dictionary<object, object> filters)
        {
            var result = _users.AsQueryable().Where(filters).FirstOrDefault();
            return null;
        }
    }
    public class User 
    {
        public string FirstName { get; set; }
        public string MiddleName { get; set; }
        public string LastName { get; set; }
        public string Email { get; set; }
    }
}

Extensions.cs

using System;
using System.Collections.Generic;
using System.Linq;
using System.Linq.Expressions;
using System.Reflection;

namespace ConsoleApplication1
{
    public static class Extensions {
        public static IQueryable<T> Where<T>(this IQueryable<T> source, string propertyName, object value) {
            return (IQueryable<T>)Where((IQueryable)source, propertyName, value);
        }

        public static IQueryable Where(this IQueryable source, string propertyName, object value) {
            var x = Expression.Parameter(source.ElementType, "x");
            var selector = Expression.Lambda(
                Expression.Equal(
                    Expression.PropertyOrField(x, propertyName),
                    Expression.Constant(value)
                ), x
            );

            return source.Provider.CreateQuery(
                Expression.Call(typeof(Queryable), "Where", new Type[] { source.ElementType }, source.Expression, selector)
            );
        }

        //NEED HELP
        //TO DO
        public static IQueryable<T> Where<T>(this IQueryable<T> source, Dictionary<object, object> filters)
        {
            return (IQueryable<T>)Where((IQueryable)source, filters);
        }

        public static IQueryable Where(this IQueryable source, Dictionary<object, object> filters)
        {
            var x = Expression.Parameter(source.ElementType, "x");
            //NEED HELP
            //expression for _users.FirstOrDefault(x=>x.Filter1==Filter1Value && x.Filter2==Filter2Value && so on and so fort depends on how many filters are passed as arguments);

            //var selector = Expression.Lambda(
            //    Expression.Equal(
            //        Expression.PropertyOrField(x, propertyName),
            //        Expression.Constant(value)
            //    ), x
            //);

            //return source.Provider.CreateQuery(
            //    Expression.Call(typeof(Queryable), "Where", new Type[] { source.ElementType }, source.Expression, selector)
            //);

            //remove line below
            return null;
        }

    }
}
4

1 に答える 1