gpt4 book ai didi

c# - 伙计,我的目标在哪里?或者,为什么 Linq 不返回我的对象​​?

转载 作者:IT王子 更新时间:2023-10-29 04:39:53 24 4
gpt4 key购买 nike

一旦获得 Linq 查询的结果,我并不总是高兴。可能会有我期待但没有出现的结果。例如,我的客户期望客户在客户列表中,但事实并非如此。是我的客户说“伙计,我的客户在哪里?”,而不是我。我是花花公子,要保持花花公子的身份,我必须给我的客户一个理由。

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

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

输出应该是这样的:

Your Customer was excluded for 2 reasons:

Customer FirstName is Carl but it should be Daniel

Customer Age is 18 but it should be > 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

}

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

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

我很担心由此产生的任何结果:

  • 可重用 - 与返回同一类对象的 Linq 查询相比,应该适用于任何对象。
  • 不影响原始查询的性能(这应该只是标准的 Linq)。
  • 应该与 Linq 实现无关。
  • 如果在丢失的实例上设置了多个属性值将其排除在结果之外,则应报告所有这些原因。

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

另外,我想知道其他人是否会觉得这有用。如果是这样,我会考虑启动一个开源项目来解决它。

最佳答案

我认为您必须将查询重新创建为 linq-to-objects 并处理 linq-to-sql/entities/whatever 和 linq-to-objects 之间的细微差别,接受一些提供者刚刚获胜'实际工作。

你有你想要在内存中找到的对象 IEnumerable<T>什么的。

你必须以某种方式遍历表达式树并剪掉叶子,所以假设你有:

where obj.foo == true && obj.bar == "yes"

你必须弄清楚 obj.foo == trueobj.bar == "yes"是叶子,从那里开始。这将是一种表达式树的深度优先搜索。

因此,构建仅具有这些叶子的对象查询的 linq。查看该对象是否包含在结果中。如果不是,那么我们已经找出了它被排除的原因,如果不是,那么就向上爬树(即使 where 查询包含更多子句,越来越接近原始子句,直到对象从结果中消失)。

在我看来,困难的部分是处理原始 linq to 'whatever' 和链接到对象之间的差异,弄清楚在哪里拆分 where 子句,处理连接之类的事情,它也可以排除事物和处理事物喜欢SqlMethods.Like在对象的 linq 中不起作用。

关于c# - 伙计,我的目标在哪里?或者,为什么 Linq 不返回我的对象​​?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12885590/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com