伙计,我的对象在哪里?或者,为什么Linq没有归还我的对象?

Dan*_*son 20 .net c# linq

一旦我得到了我的Linq查询的结果,我并不总是很开心.可能有一个结果,我期待在那里但不是.例如,我的客户希望客户在客户列表中,但事实并非如此.我的客户说"伙计,我的客户在哪里?",而不是我.我是老兄,为了成为一个老兄,我必须给我的客户原因.

是否有一种简单的方法来获取给定的对象实例和Linq查询,并确定查询中的哪些表达式排除了该实例?

编辑确定,这是一个更好的例子

输出应该是这样的:

您的客户被排除在外有两个原因:

Customer FirstName是Carl,但它应该是Daniel

客户年龄为18岁,但应该> 20岁

    public class Customer
    {
        public string FirstName { get; set; }
        public int Age { get; set; }
    }

    [Test]
    public void Dude_wheres_my_object_test1()
    {
        var daniel = new Customer { FirstName = "Daniel", Age = 41 };
        var carl =  new Customer {  FirstName = "Carl", Age= 18 };
        var Customers = new List<Customer>() { daniel, carl };

        // AsQueryable() to convert IEnumerable<T> to IQueryable<T> in 
        //the case of LinqtoObjects - only needed for this test, not 
        //production code where queies written for LinqToSql etc normally 
        //return IQueryable<T>
        var query = from c in Customers.AsQueryable()
                    where c.Age > 20
                    where c.FirstName == "Daniel"
                    select c;
        //query would return Daniel as you'd expect, but not executed here.

        //However I want to explain why Carl was not in the results

        string[] r = DudeWheresMyObject(query, carl);
        Assert.AreEqual("Age is 18 but it should be > 20", r[0]);
        Assert.AreEqual("FirstName is Carl but it should be Daniel", r[1]);


        //Should even work for a Customer who is not 
        //in the original Customers collection...
        var ficticiousCustomer = new Customer { FirstName = "Other", Age = 19};
        string[] r2= DudeWheresMyObject(query, 
                                         ficticiousCustomer);
        Assert.AreEqual("Age is 19 but it should be > 20", r2[0]);
        Assert.AreEqual("FirstName is Other but it should be Daniel", r2[1]);
    }

    public string[] DudeWheresMyObject<T>(IQueryable<T> query, T instance)
    {
        //Do something here with the query.Expression and the instance

    }
Run Code Online (Sandbox Code Playgroud)

首先,在我尝试编写一些花哨的Fluent框架之前,有没有人这样做过?

到目前为止,我已经考虑导航表达式树并针对仅包含我的对象的IQueryable执行每个分支.现在我没有很多使用原始表达树的经验,所以我希望那些不得不提出任何陷阱或甚至解释这是否是死胡同的原因.

我很担心由此产生的任何事情都应该:

  • 可重用 - 应该适用于与返回同一类对象的Linq查询进行比较的任何对象.
  • 不影响原始查询的性能(这应该只是标准的Linq).
  • 应该是Linq实现不可知的.
  • 如果在缺少的实例上设置了多个属性值,将其从结果中排除,则应报告所有这些原因.

编辑 我不建议我使用查询的不同排列多次对数据库执行LinqToSql并比较结果.相反,我正在寻找一种方法来获取单个实例并将其与表达式树进行比较(不再直接执行查询)

此外,我想了解其他人是否会发现这有用.如果是这样,我会考虑启动一个开源项目来解决它.

mlo*_*ske 0

通过一些有趣的表达式黑客,您可以查看集合中每个项目的每个阶段的评估结果。result命中断点后检查本地以查看评估结果。要实际使用评估结果,只需附加.Where(x => x.IsIncludedInResult).Select(x => x.EvaluationTarget)到生成报告的行即可。

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

namespace ConsoleApplication4
{
    [DebuggerDisplay("{Condition} on {EvaluationTarget} is {EvaluationResult}")]
    public class ReportItem<T>
    {
        public string Condition { get; private set; }

        public IEnumerable<ReportItem<T>> NestedReports { get; private set; }

        public object EvaluationResult { get; private set; }

        public T EvaluationTarget { get; private set; }

        public ReportItem(Expression condition, IEnumerable<ReportItem<T>> nestedReports, T evaluationTarget, object evaluationResult)
        {
            Condition = condition.ToString();
            NestedReports = nestedReports;
            EvaluationTarget = evaluationTarget;
            EvaluationResult = evaluationResult;
        }

        public override string ToString()
        {
            return string.Format("{0} on {1} is {2}", Condition, EvaluationTarget, EvaluationResult);
        }
    }

    [DebuggerDisplay("Included: {IsIncludedInResult} \n{Summary}")]
    public class Report<T>
    {
        public ReportItem<T> Contents { get; private set; }

        public T EvaluationTarget { get; private set; }

        public Report(T source, Expression<Func<T, bool>> predicate)
        {
            EvaluationTarget = source;

            IsIncludedInResult = predicate.Compile()(source);

            Contents = Recurse(predicate.Parameters.Single(), predicate.Body, source);
        }

        private object Evaluate(Expression expression, ParameterExpression parameter, T source)
        {
            var expr = Expression.Lambda(expression, parameter);
            var @delegate = expr.Compile();
            var value = @delegate.DynamicInvoke(source);
            return value;
        }

        private ReportItem<T> Recurse(ParameterExpression parameter, Expression sourceExpression, T source)
        {
            var constantExpression = sourceExpression as ConstantExpression;

            if(constantExpression != null)
            {
                return new ReportItem<T>(sourceExpression, null, source, Evaluate(constantExpression, parameter, source));
            }

            var unaryExpression = sourceExpression as UnaryExpression;

            if(unaryExpression != null)
            {
                var content = Recurse(parameter, unaryExpression.Operand, source);
                var result = Evaluate(sourceExpression, parameter, source);
                return new ReportItem<T>(sourceExpression, new[]{content}, source, result);
            }

            var binaryExpression = sourceExpression as BinaryExpression;

            if(binaryExpression != null)
            {
                var left = Recurse(parameter, binaryExpression.Left, source);
                var right = Recurse(parameter, binaryExpression.Right, source);
                var item = new ReportItem<T>(sourceExpression, new[] {left, right}, source, Evaluate(sourceExpression, parameter, source));
                return item;
            }

            var methodCallExpression = sourceExpression as MethodCallExpression;

            if(methodCallExpression != null)
            {
                var args = methodCallExpression.Arguments.Select(x => Evaluate(x, parameter, source)).ToArray();
                var result = methodCallExpression.Method.Invoke(Expression.Lambda(methodCallExpression.Object, parameter).Compile().DynamicInvoke(source), args);
                return new ReportItem<T>(sourceExpression, null, source, result);
            }

            throw new Exception("Unhandled expression type " + sourceExpression.NodeType + " encountered");
        }

        public bool IsIncludedInResult { get; private set; }

        public string Summary
        {
            get { return Contents.ToString(); }
        }

        public override string ToString()
        {
            return Summary;
        }
    }

    public static class PredicateRunner
    {
        public static IEnumerable<Report<T>> Report<T>(this IEnumerable<T> set, Expression<Func<T, bool>> predicate)
        {
            return set.Select(x => new Report<T>(x, predicate));
        }
    }

    class MyItem
    {
        public string Name { get; set; }

        public int Value { get; set; }

        public override int GetHashCode()
        {
            return Value % 2;
        }

        public override string ToString()
        {
            return string.Format("Name: \"{0}\" Value: {1}", Name, Value);
        }
    }

    class Program
    {
        static void Main()
        {
            var items = new MyItem[3];
            items[0] = new MyItem
            {
                Name = "Hello",
                Value = 1
            };

            items[1] = new MyItem
            {
                Name = "Hello There",
                Value = 2
            };
            items[2] = new MyItem
            {
                Name = "There",
                Value = 3
            };

            var result = items.Report(x => !x.Name.Contains("Hello") && x.GetHashCode() == 1).ToList();
            Debugger.Break();
        }
    }
}
Run Code Online (Sandbox Code Playgroud)