LINQ - 删除表达式树的部分内容

发布于 2024-11-30 00:45:37 字数 448 浏览 2 评论 0原文

我有以下 LINQ 查询:

var query = session.Query<Event>()
    .Fetch(e => e.Venue); // Fetch is an IQueryable extension which does a join

// Code needed here to remove the fetch part

var num = query.Count(); // This then hits the database

不幸的是,这会引发错误,因为计数方法不支持 fetch。在这个阶段,我确信您在想为什么我不删除 fetch 部分。然而我已经简化了我的例子,这是不可能的。理想情况下,我希望能够导航 LINQ 查询的表达式树,并在调用 Count 之前删除对 Fetch 的任何调用。

如果有人能证明这是如何可能的,我将不胜感激。谢谢

I have the following LINQ query:

var query = session.Query<Event>()
    .Fetch(e => e.Venue); // Fetch is an IQueryable extension which does a join

// Code needed here to remove the fetch part

var num = query.Count(); // This then hits the database

Unfortunately this throws an error as fetch is not supported for a count method. At this stage i'm sure you're thinking why don't i remove the fetch part. However i have simplified my example and this is not possible. What i'd ideally like to be able to do is navigate the expression tree for the LINQ query and remove any calls to Fetch before i call Count.

I'd appreciate it if someone could show how this is possible. Thanks

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

故事和酒 2024-12-07 00:45:37

可以在运行时更改表达式树(通过从现有表达式树中构建新表达式树),因为 LINQ to SQL 和实体框架等 O/RM 工具不断执行此操作,但这并不容易。随着 .NET 4.0 的 ExpressionVisitor 类的引入,它变得更加容易,但仍然不要指望它会很简单。

这是一篇文章,显示一个例子。

It is possible to change expression trees at runtime (by building a new one out of the existing one), since O/RM tools such as LINQ to SQL and Entity Framework do this constantly, but it's not really easy. It has become easier with the introduction of the ExpressionVisitor class of .NET 4.0, but still don't expect it to be simple.

Here is an article that shows an example of this.

葮薆情 2024-12-07 00:45:37

将查询转换为 Enumerable 并像这样调用 Count() 是否有帮助:

var num = query.AsEnumerable().Count();

这将执行查询,然后对结果进行简单的 Count(),而不是让 Count() 流入 ExpressionTree。

Would it help to convert the query to an Enumerable and call Count() on this like this:

var num = query.AsEnumerable().Count();

This would execute the query and afterwards makes a simple Count() on the result instead of letting the Count() flow into the ExpressionTree.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文