簡體   English   中英

Expression.Call和Count

[英]Expression.Call and Count

我正在尋找一種動態關注的方法:

var q = context.Subscription
               .Include("Client")
               .Include("Invoices")
                Where(s=>s.Client.Invoices.Count(i=>i.InvoiceID == SomeInt) > 0);

我想為左側動態構建表達式:

Expression left = s => s.Client.Invoices.Count(i => i.InvoiceID == iSomeVar); //!
Expression right = Expression.Constant(0);
var binary = Expression.GreaterThan(left, right);

謝謝!

更新的說明:

請注意:最終結果必須是

Expression<Func<T, bool>>

簡單版本:

// To give clear idea, all what I want to achieve is to determine 
// whether specific record exists in reference table using known Path. 
// Ultimately I want to extend following function (which works great by 
// the way, but for simple operations)

static Expression CreateExpression<T>(string propertyPath, 
                                      object propertyValue, 
                                      ParameterExpression parameterExpression)
{
     PropertyInfo property = typeof(T).GetProperty(propertyName);
     MemberExpression left = Expression.Property(parameterExpression, property);
     ConstantExpression right = Expression.Constant(0);
     BinaryExpression binary = Expression.GreaterThan(left, right);

     return binary;
}

// And I want to call this function and get result exactly as shown below:

Expression result = 
           CreateExpression<Subscription>("Client.Invoices.InvoiceID", 
                                          theID,
                                          valueSelector.Parameters.Single());

// Where result will be: 
//       t => t.Client.Invoices.Count(i => i.InvoiceID == theID) > 0;

擴大的視野:

// 1) I'm using Silverlight 4, EF, RIA.

// 2) At the server side I have a function GetSubscriptionsByCriteria
//   that looks about it:

public IQueryable<Subscription> GetSubscriptionsByCriteria(...)
{
      var query = this.ObjectContext.Subscriptions.Include("Client")
                                                  .Include("Client.Invoices");
      var criteria = BuildCriteria(...);
      return query.Where(criteria)
}

// 3) BuildCriteria(...) function gathers Expressions and 
//    aggregates it into the single Expression with different 
//    AND/OR conditions, something like that:

public Expression<Func<Subscription, bool>> BuildCriteria(
                      List<SearchFilter> filters,
                      Expression<Func<Subscription, bool>> valueSelector)
{
    List<Expression> filterExpressions = new List<Expression>();
    ...
    Expression expr = CreateExpression<Subscription>(
                                   sfItem.DBPropertyName, 
                                   sfItem.DBPropertyValue, 
                                   paramExpression, 
                                   sf.SearchCondition);
    filterExpressions.Add(expr);
    ...

    var filterBody = 
        filterExpressions.Aggregate<Expression>(
                (accumulate, equal) => Expression.And(accumulate, equal));
   return Expression
           .Lambda<Func<Subscription, bool>>(filterBody, paramExpression);
}

// 4) Here is the simplified version of CreateExpression function:

 static Expression CreateExpression<T>(string propertyName, 
                                       object propertyValue, 
                                       ParameterExpression paramExpression)
 {
        PropertyInfo property = typeof(T).GetProperty(propertyName);
        ConstantExpression right = Expression.Constant(0);
        MemberExpression left = Expression.Property(paramExpression, property);

        return binary = Expression.Equals(left, right);
 }

所以,我希望現在很清楚為什么我需要在原帖中左側使用Expression。 試圖盡可能將其作為DRY。

PS不要讓它太混亂這里是為什么我認為我需要做ёExpression.Call(...)ё:當我運行以下代碼並打破它看到DebugView我注意到這一點:

Expression<Func<Subscription, bool>> predicate = 
           t => t.Client.Invoices.Count(i => i.InvoiceID == 5) > 0;
BinaryExpression eq = (BinaryExpression)predicate.Body;
var left = eq.Left; // <-- See DEBUG VIEW
var right = eq.Right;   

// DEBUG VIEW:
// Arguments: Count = 2
//            [0] = {t.Client.Invoices}
//            [1] = {i => (i.InvoiceID == 5)}
// DebugView: ".Call System.Linq.Enumerable.Count(
//               ($t.Client).ClientInvoices,
//               .Lambda#Lambda1<System.Func`2[SLApp.Web.Invoice,System.Boolean]>)
//               .Lambda#Lambda1<System.Func`2[SLApp.Web.Invoice,System.Boolean]>
//               (SLApp.Web.ClientInvoice $i){ $i.ClientInvoiceID == 5 }"

這是一個構建Linq Expression的工作程序

{(x.Children.Count(y => y.SomeID == SomeVar) > 0)}
using System;
using System.Linq;
using System.Linq.Expressions;

namespace ExpressionTree
{
    class Program
    {
        static void Main(string[] args)
        {
            ParameterExpression foundX = Expression.Parameter(typeof(Parent), "x");
            Guid[] guids = new Guid[1] { Guid.NewGuid() };

            Expression expression = GetCountWithPredicateExpression(guids, foundX);
        }

        private static Expression GetCountWithPredicateExpression(Guid[] idsToFilter, ParameterExpression foundX)
        {
            System.Reflection.PropertyInfo childIDPropertyInfo = typeof(Child).GetProperty(nameof(Child.SomeID));
            ParameterExpression foundY = Expression.Parameter(typeof(Child), "y");

            Expression childIDLeft = Expression.Property(foundY, childIDPropertyInfo);
            Expression conditionExpression = Expression.Constant(false, typeof(bool));

            foreach (Guid id in idsToFilter)
                conditionExpression = Expression.Or(conditionExpression, Expression.Equal(childIDLeft, Expression.Constant(id)));

            Expression<Func<Child, bool>> idLambda = Expression.Lambda<Func<Child, bool>>(conditionExpression, foundY);

            var countMethod = typeof(Enumerable).GetMethods()
                .First(method => method.Name == "Count" && method.GetParameters().Length == 2)
                .MakeGenericMethod(typeof(Child));

            System.Reflection.PropertyInfo childrenPropertyInfo = typeof(Parent).GetProperty("Children");
            Expression childrenLeft = Expression.Property(foundX, childrenPropertyInfo);

            Expression ret = Expression.GreaterThan(Expression.Call(countMethod, childrenLeft, idLambda), Expression.Constant(0));

            return ret;
        }
    }

    public class Parent
    {
        public Child[] Children { get; set; }
    }

    public class Child
    {
        public int ID { get; set; }
        public Guid SomeID { get; set; }
    }
}

這是一個可以完成我認為您喜歡的工作程序。 它定義了一個函數,該函數獲取集合內部的整數屬性的路徑和整數值。 然后它檢查該集合是否具有該值的Count> 0。

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

namespace Test_Console
{
    public class Subscription
    {
        public int Id { get; set; }
        public Client Client { get; set; }
    }

    public class Client
    {
        public ICollection<Invoice> Invoices { get; set; }
    }

    public class Invoice
    {
        public int Id { get; set; }
    }

    class Program
    {
        static void Main(string[] args)
        {
            var subscriptions = new[]
            {
                new Subscription { Id = 1, Client = new Client { Invoices = new [] {
                    new Invoice { Id = 1 },
                    new Invoice { Id = 2 },
                    new Invoice { Id = 5 }
                } } },
                new Subscription { Id = 2, Client = new Client { Invoices = new [] {
                    new Invoice { Id = 4 },
                    new Invoice { Id = 5 },
                    new Invoice { Id = 5 }
                } } },
                new Subscription { Id = 3, Client = new Client { Invoices = new Invoice[] {
                } } },
            };

            var propertyPath = "Client.Invoices.Id";
            Console.WriteLine("What Id would you like to check " + propertyPath + " for?");
            var propertyValue = int.Parse(Console.ReadLine());
            var whereNumberOne = makeWhere<Subscription>(propertyPath, propertyValue);

            Console.WriteLine("The following Subscription objects match:");
            foreach (var s in subscriptions.Where(whereNumberOne).ToList())
            {
                Console.WriteLine("Id: " + s.Id);
            }
        }

        private static Func<T, bool> makeWhere<T>(string propertyPath, int propertyValue)
        {
            string[] navigateProperties = propertyPath.Split('.');

            var currentType = typeof(T);
            var functoidChain = new List<Func<object, object>>();
            functoidChain.Add(x => x);  // identity function starts the chain
            foreach (var nextProperty in navigateProperties)
            {
                // must be inside loop so the closer on the functoids works properly
                PropertyInfo nextPropertyInfo;

                if (currentType.IsGenericType
                 && currentType.GetGenericTypeDefinition().GetInterfaces().Contains(typeof(IEnumerable)))
                {
                    nextPropertyInfo = currentType.GetGenericArguments()[0].GetProperty(nextProperty);
                    functoidChain.Add(x =>
                        ((IEnumerable<object>)x)
                        .Count(y => (int)nextPropertyInfo.GetValue(y, null) == propertyValue)
                    );
                }
                else
                {
                    nextPropertyInfo = currentType.GetProperty(nextProperty);
                    functoidChain.Add(x => nextPropertyInfo.GetValue(x, null));
                }
                currentType = nextPropertyInfo.PropertyType;
            }
            // compose the functions together
            var composedFunctoidChain = functoidChain.Aggregate((f, g) => x => g(f(x)));
            var leftSide = new Func<T, int>(x => (int)composedFunctoidChain(x));
            return new Func<T, bool>(r => leftSide(r) > 0);
        }
    }
}

我認為這應該讓你更接近你的目標:

static Expression<Func<T, bool>> CreateAnyExpression<T, T2>(string propertyPath, 
                                    Expression<Func<T2, bool>> matchExpression)
{
    var type = typeof(T);
    var parameterExpression = Expression.Parameter(type, "s");
    var propertyNames = propertyPath.Split('.');
    Expression propBase = parameterExpression;
    foreach(var propertyName in propertyNames)
    {
        PropertyInfo property = type.GetProperty(propertyName);
        propBase = Expression.Property(propBase, property);
        type = propBase.Type;
    }
    var itemType = type.GetGenericArguments()[0];
    // .Any(...) is better than .Count(...) > 0
    var anyMethod = typeof(Enumerable).GetMethods()
        .Single(m => m.Name == "Any" && m.GetParameters().Length == 2)
        .MakeGenericMethod(itemType);
    var callToAny = Expression.Call(anyMethod, propBase, matchExpression);
    return Expression.Lambda<Func<T, bool>>(callToAny, parameterExpression);
}

這樣稱呼它:

CreateAnyExpression<Subscription, Invoice>("Client.Invoices", i => i.InvoiceID == 1)

...產生以下Expression<Func<Subscription,bool>>

s => s.Client.Invoices.Any(i => (i.InvoiceID == 1)) 

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM