# ترتيب تسلسل البيانات الجزء 1-1: قائمة التجاد مع EF

<!--category-- Entity Framework, PostgreSQL, EF Hierarchies -->
<datetime class="hidden">2025-12-06T09:10</datetime>

قائمة الترادف هي النهج البسيط والأكثر بديهية لتخزين البيانات الهرمية - كل صف يشير فقط إلى أبوه. هذا هو ما يصل إليه معظم المطورين للأول، وبالنسبة للأشجار الضحلة ذات التحركات المتكررة، هو الخيار الصحيح في كثير من الأحيان. هذه المقالة تغطي تفاصيل التنفيذ، بما في ذلك CTEs التكرارية لتجاوز الشجرة وبناء الهياكل المُعَسَّسة لعرض UI.

## السلسلة

- [الجزء 1: لمحة عامة](/blog/efcore-hierarchical-data) - التعريف والمقارنة
- **الجزء 1-1: قائمة الحدود** (هذه المادة)
- [الجزء الثاني: جدول النتائج](/blog/efcore-hierarchical-data-closure)
- [الجزء 1-3: الدرب المادي](/blog/efcore-hierarchical-data-path)
- [الجزء 1-4: المجموعات المُطرَدة](/blog/efcore-hierarchical-data-nested)
- [الجزء الأول: الشارع](/blog/efcore-hierarchical-data-ltree)

---


## ما هي قائمة التطابق؟

نموذج قائمة الترادف هو النهج الذي يصل إليه معظم المطورين للأول، و لسبب وجيه - هو بديهي. كل عقدة ببساطة تقوم بتخزين إشارة لوالدها. إذا كنت قد رسمت شجرة عائلية، فأنت بالفعل قد فهمت هذا النمط.

السمة الرئيسية: **كلّ صف يعرف فقط عن ذلك مباشرة**لتجد أجدادك أو أحفادك تحتاج إلى العديد من النظرات أو الإستفسارات المتكررة

هذا هو أكثر الأنماط توثيقاً وأكثرها دعماً جيداً من قبل [EF COre لـ](https://learn.microsoft.com/en-us/ef/core/modeling/relationships/self-referencing).

[TOC]

## كُلْ

الكيان بسيط بشكل ملحوظ - نحن فقط نضيف مرجع ذاتي قابل للإلغاء:

```csharp
public class Comment
{
    public int Id { get; set; }
    public string Content { get; set; } = string.Empty;
    public string Author { get; set; } = string.Empty;
    public DateTime CreatedAt { get; set; }

    // Foreign key - which blog post this comment belongs to
    public int PostId { get; set; }
    public BlogPost Post { get; set; } = null!;

    // ========== ADJACENCY LIST: The hierarchy is defined by these three properties ==========

    // ParentCommentId is nullable because:
    // - Root-level comments (direct replies to the post) have NULL
    // - Nested replies have the ID of the comment they're replying to
    public int? ParentCommentId { get; set; }

    // Navigation property to load the parent comment when needed
    // Useful for breadcrumb trails: "Post > Comment by Alice > Comment by Bob"
    public Comment? ParentComment { get; set; }

    // Navigation property to load immediate children (NOT grandchildren!)
    // EF Core populates this when you use .Include(c => c.Children)
    // NOTE: This only gives you ONE level deep - you won't see replies to replies
    public ICollection<Comment> Children { get; set; } = new List<Comment>();
}
```

## إف إف احتياطي

الـ تشكيل set ذاتي إشاري علاقة و بشكل حاسم إضافة لفهارس للاستفسارات نحن سَنُديرُ في أغلب الأحيان:

```csharp
public class CommentConfiguration : IEntityTypeConfiguration<Comment>
{
    public void Configure(EntityTypeBuilder<Comment> builder)
    {
        builder.HasKey(c => c.Id);

        builder.Property(c => c.Content)
            .IsRequired()
            .HasMaxLength(10000);

        builder.Property(c => c.Author)
            .IsRequired()
            .HasMaxLength(200);

        // Standard relationship: comment belongs to a blog post
        // Cascade delete here is safe - deleting a post should remove all its comments
        builder.HasOne(c => c.Post)
            .WithMany(p => p.Comments)
            .HasForeignKey(c => c.PostId)
            .OnDelete(DeleteBehavior.Cascade);

        // ========== THE SELF-REFERENCING RELATIONSHIP ==========
        // This is what makes it an adjacency list - each comment points to its parent

        builder.HasOne(c => c.ParentComment)
            .WithMany(c => c.Children)           // One parent has many children
            .HasForeignKey(c => c.ParentCommentId)
            .OnDelete(DeleteBehavior.Restrict);  // WARNING: Don't use Cascade here!

        // Why Restrict and not Cascade?
        // With Cascade, deleting a parent would automatically delete ALL children,
        // grandchildren, etc. This can be:
        // 1. Unexpected behaviour for users
        // 2. A database performance issue (many deletes)
        // 3. A data integrity risk (accidental mass deletion)
        // Better to handle subtree deletion explicitly in application code

        // ========== INDEXES: Critical for performance ==========

        // Index on ParentCommentId - used when loading children
        // "SELECT * FROM comments WHERE parent_comment_id = @id"
        builder.HasIndex(c => c.ParentCommentId);

        // Index on PostId - used when loading all comments for a post
        // "SELECT * FROM comments WHERE post_id = @id"
        builder.HasIndex(c => c.PostId);

        // Composite index for the most common query:
        // "Get all comments for a post, ordered by creation date"
        builder.HasIndex(c => new { c.PostId, c.CreatedAt });
    }
}
```

## قاعدة بيانات

والمخطط الناتج عن ذلك هو الحد الأدنى - مجرد مفتاح أجنبي واحد للاشارة الذاتية:

```mermaid
erDiagram
    COMMENT {
        int id PK
        string content
        string author
        datetime created_at
        int post_id FK
        int parent_comment_id FK "nullable - NULL for root comments"
    }

    BLOG_POST {
        int id PK
        string title
        string content
    }

    BLOG_POST ||--o{ COMMENT : "has"
    COMMENT ||--o{ COMMENT : "has children"
```

## 

### إدخال جديد

تدرجات هي بشكل جميل بسيطة - نمط قائمة التوازِيّة يُشَارِعُ حقاً هنا:

```csharp
public async Task<Comment> AddCommentAsync(
    int postId,
    int? parentId,      // NULL for root comment, parent's ID for a reply
    string author,
    string content,
    CancellationToken ct = default)
{
    // Creating a comment is just setting the parent reference
    // No need to update closure tables, recalculate paths, or renumber anything
    var comment = new Comment
    {
        PostId = postId,
        ParentCommentId = parentId,  // This single reference defines the hierarchy
        Author = author,
        Content = content,
        CreatedAt = DateTime.UtcNow
    };

    context.Comments.Add(comment);
    await context.SaveChangesAsync(ct);

    logger.LogInformation("Added comment {CommentId} to post {PostId}", comment.Id, postId);
    return comment;
}
```

### إحصل على الأطفال المُوَجِّهين

استعلام - سريع وبسيط:

```csharp
public async Task<List<Comment>> GetChildrenAsync(int commentId, CancellationToken ct = default)
{
    // This is WHERE adjacency lists shine - getting children is trivial
    // Single indexed lookup: WHERE parent_comment_id = @id
    return await context.Comments
        .AsNoTracking()                              // Read-only, no tracking overhead
        .Where(c => c.ParentCommentId == commentId)  // Uses the index we defined
        .OrderBy(c => c.CreatedAt)                   // Chronological order
        .ToListAsync(ct);
}
```

### الحصول على الأسلاف (الجزء الصعب)

هنا حيث تظهر قوائم المفارقة ضعفهم. بدون استفسارات متكررة، سيكون عليك القيام برحلات متعددة ذهابا وإيابا: الحصول على الوالد، الحصول على والد الوالد، الحصول على جده... وهلم جرا.

شكرًا جزيلًا لـ PostgresQL [TTEs](https://www.postgresql.org/docs/current/queries-with.html#QUERIES-WITH-RECURSIVE) (أ) على سبيل الانقاذ:

```csharp
public async Task<List<Comment>> GetAncestorsAsync(int commentId, CancellationToken ct = default)
{
    // WHY RAW SQL?
    // EF Core doesn't have great support for recursive CTEs
    // We need to drop down to raw SQL for this

    // HOW THE CTE WORKS:
    // 1. Start with the target comment (WHERE id = {0})
    // 2. UNION ALL joins each result with its parent (JOIN on parent_comment_id)
    // 3. PostgreSQL keeps doing this until no more parents are found
    // 4. We exclude the starting comment (WHERE id != {0}) to get only ancestors

    var sql = @"
        WITH RECURSIVE ancestors AS (
            -- Base case: start with our target comment
            SELECT * FROM comments WHERE id = {0}

            UNION ALL

            -- Recursive case: join each result with its parent
            SELECT c.*
            FROM comments c
            INNER JOIN ancestors a ON c.id = a.parent_comment_id
        )
        -- Return all ancestors except the starting comment, in ID order (root first)
        SELECT * FROM ancestors WHERE id != {0}
        ORDER BY id";

    return await context.Comments
        .FromSqlRaw(sql, commentId)
        .AsNoTracking()
        .ToListAsync(ct);
}
```

### إحصل على كامل جزء فرعي مع تعمّد

يتطلب أيضًا CTE تكراري، لكن هذه المرة نتتبع العمق بينما نحن نذهب:

```csharp
public async Task<List<CommentWithDepth>> GetDescendantsWithDepthAsync(
    int commentId,
    CancellationToken ct = default)
{
    // Similar to ancestors, but we go DOWN the tree instead of UP
    // We also track depth so we know how to indent in the UI

    var sql = @"
        WITH RECURSIVE descendants AS (
            -- Base case: start with our target comment at depth 0
            SELECT *, 0 as depth FROM comments WHERE id = {0}

            UNION ALL

            -- Recursive case: find children of each result, incrementing depth
            SELECT c.*, d.depth + 1
            FROM comments c
            INNER JOIN descendants d ON c.parent_comment_id = d.id
        )
        -- Return all descendants, ordered for display
        -- depth first, then by creation time within each level
        SELECT id, content, author, created_at, post_id, parent_comment_id, depth
        FROM descendants
        WHERE id != {0}
        ORDER BY depth, created_at";

    // Note: We need a special DTO to capture the depth column
    // EF Core's FromSqlRaw won't automatically map extra columns to entity properties
    return await context.Database
        .SqlQueryRaw<CommentWithDepth>(sql, commentId)
        .ToListAsync(ct);
}

// DTO to hold comment data plus computed depth
public class CommentWithDepth
{
    public int Id { get; set; }
    public string Content { get; set; } = string.Empty;
    public string Author { get; set; } = string.Empty;
    public DateTime CreatedAt { get; set; }
    public int PostId { get; set; }
    public int? ParentCommentId { get; set; }
    public int Depth { get; set; }  // Computed by the CTE
}
```

### مبنى مبنى مُزْدَدَدْرَج

في كثير من الأحيان ما تحتاجه في الواقع للترجمة هو هيكل شجرة، وليس قائمة مسطحة. هنا كيفية بناء واحد بكفاءة:

```csharp
public async Task<List<CommentTreeNode>> GetCommentTreeAsync(int postId, CancellationToken ct = default)
{
    // STRATEGY:
    // 1. Load ALL comments for the post in a single query (fast, one round trip)
    // 2. Build the tree structure in memory (also fast, just pointer manipulation)

    // Step 1: Get all comments for this post
    var allComments = await context.Comments
        .AsNoTracking()
        .Where(c => c.PostId == postId)
        .OrderBy(c => c.CreatedAt)  // Consistent ordering
        .ToListAsync(ct);

    // Step 2: Create a lookup by parent ID
    // This gives us O(1) access to children of any comment
    var lookup = allComments.ToLookup(c => c.ParentCommentId);

    // Step 3: Build the tree starting from root comments (ParentCommentId = null)
    return BuildTree(lookup, null);
}

private List<CommentTreeNode> BuildTree(
    ILookup<int?, Comment> lookup,
    int? parentId)
{
    // Recursively build tree nodes
    // lookup[parentId] gives us all comments whose parent is 'parentId'
    return lookup[parentId]
        .Select(c => new CommentTreeNode
        {
            Comment = c,
            Children = BuildTree(lookup, c.Id)  // Recurse to get children
        })
        .ToList();
}

public class CommentTreeNode
{
    public Comment Comment { get; set; } = null!;
    public List<CommentTreeNode> Children { get; set; } = new();

    // Convenience property for UI
    public bool HasChildren => Children.Count > 0;
}
```

### احذف اصغر

يتطلب الإزاحة إيجاد كل الأحفاد أولاً:

```csharp
public async Task DeleteSubtreeAsync(int commentId, CancellationToken ct = default)
{
    // We need to find and delete all descendants, then the comment itself
    // Using a CTE to get all IDs, then bulk delete

    var sql = @"
        WITH RECURSIVE subtree AS (
            SELECT id FROM comments WHERE id = {0}
            UNION ALL
            SELECT c.id FROM comments c
            INNER JOIN subtree s ON c.parent_comment_id = s.id
        )
        DELETE FROM comments WHERE id IN (SELECT id FROM subtree)";

    var deleted = await context.Database.ExecuteSqlRawAsync(sql, new object[] { commentId }, ct);

    logger.LogInformation("Deleted {Count} comments in subtree rooted at {CommentId}",
        deleted, commentId);
}
```

### 

هنا حيث قوائم التقاء التألق تألق حقاً - التحرك هو تافه:

```csharp
public async Task MoveSubtreeAsync(
    int commentId,
    int newParentId,
    CancellationToken ct = default)
{
    // In adjacency list, moving a subtree is just updating ONE row!
    // All descendants automatically move with their parent because
    // their ParentCommentId still points to their direct parent

    var comment = await context.Comments.FindAsync(new object[] { commentId }, ct);
    if (comment == null)
    {
        throw new InvalidOperationException($"Comment {commentId} not found");
    }

    // Prevent creating a cycle (moving a node under its own descendant)
    // This would create an infinite loop in our tree
    var ancestors = await GetAncestorsAsync(newParentId, ct);
    if (ancestors.Any(a => a.Id == commentId))
    {
        throw new InvalidOperationException("Cannot move a comment under its own descendant");
    }

    comment.ParentCommentId = newParentId;
    await context.SaveChangesAsync(ct);

    logger.LogInformation("Moved comment {CommentId} to new parent {NewParentId}",
        commentId, newParentId);
}
```

## & & است السؤال

```mermaid
sequenceDiagram
    participant App as Application
    participant EF as EF Core
    participant DB as PostgreSQL

    Note over App,DB: Getting Children (Simple - O(1))
    App->>EF: GetChildrenAsync(commentId)
    EF->>DB: SELECT * FROM comments WHERE parent_id = @id
    DB-->>EF: Results (indexed lookup)
    EF-->>App: List<Comment>

    Note over App,DB: Getting Ancestors (Recursive - O(d) where d=depth)
    App->>EF: GetAncestorsAsync(commentId)
    EF->>DB: WITH RECURSIVE ancestors AS (...)
    DB->>DB: Traverse parent_id chain recursively
    DB-->>EF: All ancestors
    EF-->>App: List<Comment>
```

## (أ)

□ العملية □ معقّد □ رحلات جولة لقاعدة البيانات □ ملحوظات □
|-----------|------------|---------------------|-------|
تُدرج o o o (1) o 1 /
* الحصول على أطفال * O (1) / 1 بحث مفهرس *
o يحصل على أسلاف o O(d) o 1 (مع CTE) o d = العمق، تعمل CTE في DB o
o الحصول على شجرة فرعية o O(n) o O(n) o 1 (مع CTE) n = حجم شجرة فرعية o
احدث صفا واحدا فقط
تُحذف الفقرة الفرعية o o O(n) o o 1 (مع CTE) n = حجم الشجرة الفرعي o

## بروات وكونس

▪ pros cons /
|------|------|
• يتطلب توخي البساطة في فهم وتنفيذ ما يتطلبه الحصول على الأجداد/المنحدرين من استفسارات متكررة
يمكن أن تكون هذه العمليات بطيئة على الأشجار العميقة جداً □
نقل شجرة صغيرة (تحديث صف واحد) □ لا توجد طريقة سهلة للحصول على عمق دون التنقّل □
خصائص الملاحة الأساسية تعمل بشكل طبيعي لا يمكن بسهولة عد ذرات دون تحميلها
□ لا يوجد فائض في البيانات للإبقاء على الاستفسارات المتكررة بشأن مشاكل N+1 إن لم تكن حذرة □

## إلى تاريخ استخدام قائمة مُززج

****

- تسلسلك الهرمي سطحي (أقل من 5-6 مستويات)
- أنت كثيراً ما تحرّل
- تريد من EF COre أن تعمل بشكل طبيعي
- المتطلبات البسيطة التي تكون فيها هذه المواد مقبولة
- يُدرج أداء أكثر أهمية مما يُقرأ الأداء

**مُفكّر مُزْرِج %s:**

- لديك تسلسل هرمي عميق (10 مستويات + 10)
- تَتسائلُ كثيراً "كُلّ أحفاد" أَو "كُلّ أسلاف"
- أداء القراءة المقروءة أمر حاسم الأهمية
- تحتاج إلى عد الأحفاد من دون تحميلهم

## السلسلة

- [الجزء 1: لمحة عامة](/blog/efcore-hierarchical-data)
- **الجزء 1-1: قائمة الحدود** (هذه المادة)
- [الجزء الثاني: جدول النتائج](/blog/efcore-hierarchical-data-closure)
- [الجزء 1-3: الدرب المادي](/blog/efcore-hierarchical-data-path)
- [الجزء 1-4: المجموعات المُطرَدة](/blog/efcore-hierarchical-data-nested)
- [الجزء الأول: الشارع](/blog/efcore-hierarchical-data-ltree)