- r - 以节省内存的方式增长 data.frame
- ruby-on-rails - ruby/ruby on rails 内存泄漏检测
- android - 无法解析导入android.support.v7.app
- UNIX 域套接字与共享内存(映射文件)
一旦获得 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 执行每个分支。现在我没有太多使用原始表达式树的经验,所以我希望那些必须提出任何陷阱甚至解释这是否是死胡同以及原因的人。
我很担心由此产生的任何结果:
编辑我并不是建议我使用不同的查询排列多次对数据库执行 LinqToSql 并比较结果。相反,我正在寻找一种方法来获取单个实例并将其与表达式树进行比较(无需再次直接执行查询)
另外,我想知道其他人是否会觉得这有用。如果是这样,我会考虑启动一个开源项目来解决它。
最佳答案
我认为您必须将查询重新创建为 linq-to-objects 并处理 linq-to-sql/entities/whatever 和 linq-to-objects 之间的细微差别,接受一些提供者刚刚获胜'实际工作。
你有你想要在内存中找到的对象 IEnumerable<T>
什么的。
你必须以某种方式遍历表达式树并剪掉叶子,所以假设你有:
where obj.foo == true && obj.bar == "yes"
你必须弄清楚 obj.foo == true
和 obj.bar == "yes"
是叶子,从那里开始。这将是一种表达式树的深度优先搜索。
因此,构建仅具有这些叶子的对象查询的 linq。查看该对象是否包含在结果中。如果不是,那么我们已经找出了它被排除的原因,如果不是,那么就向上爬树(即使 where 查询包含更多子句,越来越接近原始子句,直到对象从结果中消失)。
在我看来,困难的部分是处理原始 linq to 'whatever' 和链接到对象之间的差异,弄清楚在哪里拆分 where 子句,处理连接之类的事情,它也可以排除事物和处理事物喜欢SqlMethods.Like
在对象的 linq 中不起作用。
关于c# - 伙计,我的目标在哪里?或者,为什么 Linq 不返回我的对象?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12885590/
我创建了一个基于命令行可移植脚本的工业化不可知构建系统,可用于快速构建多个依赖项目,而不必依赖特定的 IDE 或构建工厂。它是不可知的,因为它不是基于单个构建引擎。我使用 cmake 创建了第一个版本
我最初使用 Java 目标开发了一个语法(用于 TestRig 支持),然后将其移植到 Python(从 git hub 语法存储库扩展了 Python3 语法,因此需要将操作移植到 Python
我有一个以 iPhone 和 watchOS 为目标的 Xcode 项目。 iPhone 目标使用加速度计,模拟器不支持。我可以只启动 iPhone 应用程序而不启动 watch 目标吗?我从: Ca
您好,我想创建一个批处理文件,用于在 .eml 文件(目标 A)中查找某些关键字,然后删除它们所在的行。之后,我需要批处理文件将"new"文件放入(目标 B)中的单独 .eml 文件中。文件也可以是
当尝试通过 IntelliJ 运行示例 CorDapp (GitHub CorDapp) 时,我收到以下错误: Cannot inline bytecode built with JVM target
我在尝试向我的 kotlin spring 项目添加一些依赖项时遇到问题。我使用 spring boot 初始化程序来运行一个基本项目。 我的问题:如果我取消对 jackson 或 Koin 依赖项的
这是有问题的网站: http://www.onepixelroom.com/londonrefurb 当我点击关于部分后面的多个圆圈时,我希望它更改上面文本中的引号。 到目前为止,我得到它来显示 文本
单击后,我将删除两个元素 $(this) 和 $("#foo")。 目前我的代码如下所示: $(this).remove(); $("#foo").remove(); 如何在不重复自己的情况下优化它?
我有一个小脚本,可将 Markdown 文件编译为 html,并将其与一些样式表和 javascript 一起插入到模板的主体中。我有一个 GNU makefile 来完成这个: output.htm
已关闭。此问题需要 debugging details 。目前不接受答案。 编辑问题以包含 desired behavior, a specific problem or error, and the
一些背景知识: 在android中我们开发了同样的应用,基本上我们先开发了Android应用,现在我们创建了它的IOS版本,所以这个应用有多个客户端。在 android 中,我们实际上是使用 Andr
我想知道是否可以使用 knockout 来更改html中的目标() 我的所有其他信息都在 JavaScript 中,所以这对我来说是一个大问题。这是我的 JavaScript: var library
这个问题在这里已经有了答案: Selecting and manipulating CSS pseudo-elements such as ::before and ::after using j
我在我的有向图中添加了一堆节点和顶点,使用设置 typedef boost::adjacency_list graph; 创建 Node有一个节点名称字符串,Edge它的分数有一个整数。我试图遍历所有
关闭。这个问题不符合Stack Overflow guidelines .它目前不接受答案。 这个问题似乎与 help center 中定义的范围内的编程无关。 . 关闭 8 年前。 Improve
如何存储我在 NSUserDefaults 中创建的 Goal 类型的对象数组? ( swift ) 代码如下: func saveGoalList ( newGoalList : [Goal] ){
Array.prototype.indexOf 和 Date.now 已在 ES5 中引入。如果我编译存储在文件 test.ts 中的以下代码,为什么 Typescript 不能转译? Date.no
我正在阅读有关属性的内容,并了解到可以使用您的代码将它们应用于不同的目标实体 -(请参阅 Attribute Targets)。 因此,查看我项目中的 AssemblyInfo.cs 文件,我可以看到
给定一个 Makefile: all: build/a build/b build/c # need to change this to all: build/* build/a:
我有一个带有多框架目标的项目- netstandard2.0;net471 . 我想为 netframework 构建解决方案和 netstandard分别。 目前我使用这个 MSBuild 命令:
我是一名优秀的程序员,十分优秀!