简体   繁体   English

Linq2Sql中的自引用表

[英]Self-referencing tables in Linq2Sql

I've seen a lot of questions on self-referencing tables in Linq2Sql and how to eagerly load all child records for a particular root object. 我已经在Linq2Sql中的自引用表上看到了很多问题,以及如何急切地加载特定根对象的所有子记录。 I've implemented a temporary solution by accessing all underlying properties, but you can see that this doesn't do the performance any good. 我已经通过访问所有基础属性实现了一个临时解决方案,但是您可以看到这对性能没有任何好处。

The thing is though, that all records are correlated with each-other using a correlation GUID. 事实是,所有记录都使用关联GUID相互关联。 Example below: 下面的例子:

RootElement
- Id: 1
- ParentId: null
- CorrelationId: 4D68E512-4B55-44f4-BA5A-174B630A03DD

ChildElement1
- Id: 2
- ParentId: 1
- CorrelationId: 4D68E512-4B55-44f4-BA5A-174B630A03DD

ChildElement2
- Id: 3
- ParentId: 2
- CorrelationId: 4D68E512-4B55-44f4-BA5A-174B630A03DD

ChildElement1
- Id: 4
- ParentId: 2
- CorrelationId: 4D68E512-4B55-44f4-BA5A-174B630A03DD

In my case, I do have access to the correlationId, so I can retrieve all of my records by performing the following query: 就我而言,我确实可以访问correlationId,因此我可以通过执行以下查询来检索所有记录:

from element in db.Elements
where element.CorrelationId == '4D68E512-4B55-44f4-BA5A-174B630A03DD'
select element;

But, of course, I want these elements associated with each other by executing this query: 但是,当然,我希望通过执行以下查询将这些元素彼此关联:

from element in db.Elements
where element.CorrelationId == '4D68E512-4B55-44f4-BA5A-174B630A03DD' && element.ParentId == null
select element;

My question is: is it possible to combine the results the first query as some sort of 'caching mechanism' for the query where I get the root element? 我的问题是:是否有可能将第一个查询的结果组合为获取根元素的查询的某种“缓存机制”?

Thanks for the input. 感谢您的输入。

J. J.

I'm not quite clear on what you're trying to do, or what you mean by 'caching mechanism'. 对于您要执行的操作或“缓存机制”的含义,我不太清楚。 How does the second query create any sort of association? 第二个查询如何创建任何形式的关联?

My first hunch is that you want to group the results by parent, in which case you can simply call GroupBy(x => x.ParentId) on the results of the first query. 我的第一个预感是GroupBy(x => x.ParentId)父项对结果进行分组,在这种情况下,您可以GroupBy(x => x.ParentId)第一个查询的结果调用GroupBy(x => x.ParentId)

My second hunch is that your second query is to get only the root elements, in which case you just call Where(x => x.ParentId == null) on the results of the first query. 我的第二个预感是,第二个查询仅获取根元素,在这种情况下,您只需对第一个查询的结果调用Where(x => x.ParentId == null)

Please clarify further if this is not what you're looking for. 如果这不是您想要的,请进一步说明。

edit: 编辑:

In response to your first comment, here is how to execute the two queries: 为了回应您的第一条评论,以下是如何执行两个查询:

var correlated_elements = db.Elements.Where(element => element.CollelationId == '4D68E512-4B55-44f4-BA5A-174B630A03DD');
var root_elements = correlated_elements.Where(element => element.ParentId == null);

This is only going to make one query to the database. 这只会对数据库进行一次查询。 Still, your second query does not create a hierarchy -- it just returns the elements without a ParentId . 仍然,您的第二个查询不会创建层次结构-它只返回没有ParentId的元素。 If you can describe the hierarchy you want, we can work on a query to achieve it. 如果您可以描述所需的层次结构,那么我们可以通过查询来实现它。

If you're not sure what queries are actually being executed against the database, you can output the SQL to the console or use a trial version of LINQ-to-SQL Profiler . 如果不确定对数据库实际执行了哪些查询,则可以将SQL输出到控制台,或使用LINQ-to-SQL Profiler的试用版。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM