C
C#17mo ago
V0FBU1VM

❔ Linq Query HELP

I have this entity. During deletion, I would like to load all related entities, so that I can delete them also. Here is the entity
/// <summary>
/// Represents a comment entity.
/// </summary>
public class Comment : AbstractEntity, ISuspension
{
public required string Content { get; set; }
public int Depth { get; set; }
public Guid AuthorId { get; set; }
public virtual User Author { get; set; } = default!;
public Guid QuoteId { get; set; }
public virtual Quote Quote { get; set; } = default!;
public Guid? ParentId { get; set; }
public virtual Comment ParentComment { get; set; } = default!;
public virtual ICollection<Comment> Replies { get; set; } = new HashSet<Comment>();
public virtual ICollection<Like<Comment>> Likes { get; set; } = new HashSet<Like<Comment>>();
public virtual ICollection<Dislike<Comment>> Dislikes { get; set; } = new HashSet<Dislike<Comment>>();
public virtual ICollection<Suspension<Comment>> Suspensions { get; set; } = new HashSet<Suspension<Comment>>();
public bool IsSuspended { get; set; }
}
/// <summary>
/// Represents a comment entity.
/// </summary>
public class Comment : AbstractEntity, ISuspension
{
public required string Content { get; set; }
public int Depth { get; set; }
public Guid AuthorId { get; set; }
public virtual User Author { get; set; } = default!;
public Guid QuoteId { get; set; }
public virtual Quote Quote { get; set; } = default!;
public Guid? ParentId { get; set; }
public virtual Comment ParentComment { get; set; } = default!;
public virtual ICollection<Comment> Replies { get; set; } = new HashSet<Comment>();
public virtual ICollection<Like<Comment>> Likes { get; set; } = new HashSet<Like<Comment>>();
public virtual ICollection<Dislike<Comment>> Dislikes { get; set; } = new HashSet<Dislike<Comment>>();
public virtual ICollection<Suspension<Comment>> Suspensions { get; set; } = new HashSet<Suspension<Comment>>();
public bool IsSuspended { get; set; }
}
I have come this far
var comment = await DbContext.Comments
.Include(x => x.Replies)
.ThenInclue(x => x.Likes)
.ThenInclue(x => x.Dislikes)
.ThenInclue(x => x.Suspensions)
.Include(x => x.Likes)
.Include(x => x.Dislikes)
.Include(x => x.Suspensions)
.FirstOrDefaultAsync(x => x.Id == request.Id && x.AuthorId == UserId, cancellationToken);
var comment = await DbContext.Comments
.Include(x => x.Replies)
.ThenInclue(x => x.Likes)
.ThenInclue(x => x.Dislikes)
.ThenInclue(x => x.Suspensions)
.Include(x => x.Likes)
.Include(x => x.Dislikes)
.Include(x => x.Suspensions)
.FirstOrDefaultAsync(x => x.Id == request.Id && x.AuthorId == UserId, cancellationToken);
I would like to load the Replies, and there likes, dislikes, suspensions. I also need to load the replies of replies and there likes, dislikes, suspensions etc. Any help would be appreciated. I don't think this query will do the job.
4 Replies
Angius
Angius17mo ago
Can't you just set up the delete behaviour to cascade...?
V0FBU1VM
V0FBU1VMOP17mo ago
@ZZZZZZZZZZZZZZZZZZZZZZZZZ I have tried that, the problem is that when migrating. It's not liking it.
Omnissiah
Omnissiah17mo ago
loading all this stuff just to delete it is kind of a waste of resources if you ask me if migrations don't work because data is half deleted and half not then you have to look again at the foreign keys
Accord
Accord17mo ago
Was this issue resolved? If so, run /close - otherwise I will mark this as stale and this post will be archived until there is new activity.
Want results from more Discord servers?
Add your server