LINQ: Passing lambda expression as parameter to be executed and returned by method

前端 未结 3 630
悲&欢浪女
悲&欢浪女 2020-11-28 06:53

So here is the scenario: i have a series of different repository classes that each can use an isolated data context, or a shared context. In the cases where an isolated cont

相关标签:
3条回答
  • 2020-11-28 07:18

    Check out PredicateBuilder - http://www.albahari.com/nutshell/predicatebuilder.aspx

    This code will package up your where clause as Expression objects which you can pass about.

    I've implemented the Repository pattern, and my flava is to give it a Fetch(ICriteria) method, which builds up the Where clause depending on the criteria supplied. Simple, but works for me.

    0 讨论(0)
  • 2020-11-28 07:37

    Something like this:

    public IEnumerable<T> ExecuteInContext<T>(
      Expression<Func<T,bool>> predicate)
    {
      ... // do your stuff
      //eg
      Table<T> t = GetTable<T>();
      return t.Where(predicate);
    }
    

    or

    public IEnumerable<T> ExecuteInContext<T>(
       IQueryable<T> src, Expression<Func<T,bool>> predicate)
    {
      return src.Where(predicate);
    }
    

    Usage:

    var r = repo.ExecuteInContext<SomeType>( 
              x => x.SomeProp.Equals(Somevalue));
    

    or

    var r = repo.ExecuteInContext(GetTable<T>(), 
              x => x.SomeProp.Equals(Somevalue));
    

    Assumptions:

    1. Table can be derived from T, else you will need to pass the source too.
    2. You know how to modify the predicate expression if needed.
    0 讨论(0)
  • 2020-11-28 07:39

    Here is a complete working sample how to pass LINQ expression as a parameter

    using System;
    using System.Linq.Expressions;
    using System.Reflection;
    
    namespace ConsoleTest
    {
        public class Values
        {
            public int X { get; set; }
            public int Y { get; set; }
    
            public override string ToString()
            {
                return String.Format("[ X={0} Y={1} ]", X, Y);
            }
        }
    
        class Program
        {
            static void Main()
            {
                var values = new Values {X = 1, Y = 1};
    
                // pass parameter to be incremented as linq expression
                IncrementValue(values, v => v.X);
                IncrementValue(values, v => v.X);
                IncrementValue(values, v => v.Y);
    
                // Output is: [ X=3 Y=2 ]
                Console.Write(values);
            }
    
            private static void IncrementValue<T>(T obj, Expression<Func<T,int>> property)
            {
                var memberExpression = (MemberExpression)property.Body;
                var propertyInfo = (PropertyInfo)memberExpression.Member;
                // read value with reflection
                var value = (int)propertyInfo.GetValue(obj, null);
                // set value with reflection
                propertyInfo.SetValue(obj, ++value, null);
            }
        }
    }
    
    0 讨论(0)
提交回复
热议问题