gpt4 book ai didi

c# - LINQ 自引用表过滤关系

转载 作者:太空宇宙 更新时间:2023-11-03 10:38:22 25 4
gpt4 key购买 nike

我有 2 个表 CategoriesImages 类别是自引用的,ParentId 作为可空外键

Database Diagram Screenshot

代码优先类

public class Category {

public int Id { get; set; }
public string Name{ get; set; }
public int? ParentId { get; set; }
public bool IsDeleted { get; set; }
public byte[] Timestamp { get; set; }

public virtual Category Parent { get; set; }
public virtual ICollection<Category> Parents { get; set; }

public virtual ICollection<Image> Images { get; set; }
}


Public class Image {

public int Id { get; set; }
public int? CategoryId { get; set; }
public string Source { get; set; }
public string Description { get; set; }
public bool IsDeleted { get; set; }
public byte[] Timestamp { get; set; }

// Foreign keys
public virtual Category Category { get; set; }
}


public class CategoryMap : EntityTypeConfiguration<Category> {
public CategoryMap() {
// Primary Key
HasKey(t => t.Id);

// Properties
Property(t => t.Name).IsRequired().HasMaxLength(250);
Property(t => t.Timestamp).IsRequired().IsFixedLength().HasMaxLength(8).IsRowVersion();

// Table & Column Mappings
ToTable("Category");
Property(t => t.Id).HasColumnName("Id");
Property(t => t.ParentId).HasColumnName("ParentId");
Property(t => t.Name).HasColumnName("Category");
Property(t => t.IsDeleted).HasColumnName("IsDeleted");
Property(t => t.Timestamp).HasColumnName("Timestamp");

// Relationships
HasOptional(t => t.Parent).WithMany(t => t.Parents)
.HasForeignKey(d => d.ParentId)
.WillCascadeOnDelete(false);
}

public class ProfileImageMap : EntityTypeConfiguration<ProfileImage> {

public ProfileImageMap() {
// Primary Key
HasKey(t => t.Id);

// Properties
Property(t => t.Source).IsRequired().HasMaxLength(255);
Property(t => t.Description).HasMaxLength(255);
Property(t => t.Timestamp).IsRequired().IsFixedLength().HasMaxLength(8).IsRowVersion();

// Table & Column Mappings
ToTable("Images");
Property(t => t.Id).HasColumnName("Id");
Property(t => t.CategoryId ).HasColumnName("CategoryId ");
Property(t => t.Source).HasColumnName("Source");
Property(t => t.Description).HasColumnName("Description");
Property(t => t.IsDeleted).HasColumnName("IsDeleted");
Property(t => t.Timestamp).HasColumnName("Timestamp");

// Relationships
HasOptional(t => t.Category).WithMany(t => t.Images)
.HasForeignKey(d => d.CategoryId);
}
}

** 上下文**

    public DbSet<Category> Categories { get; set; }
public DbSet<Image> Images { get; set; }

问题是我如何构建一个 LINQ 语句来返回一个基于 Id 的类别,父类别包含所有图像,但不包括标记为 IsDeleted = true

的图像

是否可以在 LINQLinqKit 中执行此操作

最佳答案

这将返回您应用过滤器所需的所有详细信息。唯一“复杂”的部分是应用于 Images 属性的 Where 子句(我还添加了 null 检查,否则你可能会得到一个 NullReferenceException):

var categoryID = 2; //The ID you are searching for

var category = from c in categories
where c.Id == categoryID
select new Category
{
Id = c.Id,
Name = c.Name,
ParentId = c.ParentId,
IsDeleted = c.IsDeleted,
Timestamp = c.Timestamp,
Parent = c.Parent,
Images = c.Images == null ?
(ICollection<Image>)new List<Image>() :
(ICollection<Image>)c.Images.Where(i => i.IsDeleted = false).ToList()
}.Single();

如果要返回所有父类别(即遍历层次结构),那么一种方法是使用如下函数:

public IEnumerable<Category> GetParents(IEnumerable<Category> categories, Category child) 
{
List<Category> parents = new List<Category>();
var current = child.Parent;
while (current != null)
{
parents.Add(parent);
parent = parent.Parent;
}

return parents;

}

现在你可以说:

category.Parents = GetParents(categories, category);

关于c# - LINQ 自引用表过滤关系,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26539275/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com