LINQ - 递归请求 [英] LINQ - recursive request

查看:143
本文介绍了LINQ - 递归请求的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下数据对象:

Messages:
MessageID int,
Text string,
ParentMessageID int?

所以,记录如下:


1 | 文字1| null
2 | 回复文字1| 1
3 | 回复回复1| 2

1 | "Text 1" | null 2 | "Reply to Text 1" | 1 3 | "Reply to reply to text 1" | 2

...

我需要计算多少条信息一个线程。如何使用LINQ?

I need to calculate how many messages in one thread. How to do it by LINQ?

推荐答案

不可能使用LINQ to Entities:您需要编写一个常见的表表达式使用SQL执行递归查询。这些往往很慢。

It's not possible using LINQ to Entities: you would need to write a common table expression to do the recursive query using SQL. These are quite often slow.

假设您总是希望从层次结构中的同一级别进行计数,最简单的解决方案是为根ID添加属性,如此

Assuming you always want to count from the same level in the hierarchy, your easiest solution is to add a property for the root id, like this.

Messages:
MessageID int,
Text string,
ParentMessageID int?
ThreadRootID int

线程根对给定线程中的所有消息都是相同的。很容易计算那些具有相同的 ThreadRootId (并且比递归查询更好的性能)。

The thread root is the same for all messages in a given thread. It's easy to count those that have the same ThreadRootId (and will be much more performant than a recursive query).

这篇关于LINQ - 递归请求的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆