简体   繁体   English

递归Linq查询

[英]Recursive Linq query

Currently I'm working with WebApi and Entity Framework, So I have 3 entities: Products, Categories and ProductCategory; 目前,我正在使用WebApi和Entity Framework,所以我有3个实体:产品,类别和ProductCategory; their relationships are: 他们的关系是:

在此处输入图片说明

My problem is that Category entity has a Category Parent property, so it's recursive, my Category Controller looks like this: 我的问题是Category实体具有Category Parent属性,因此它是递归的,我的Category Controller如下所示:

public async Task<IHttpActionResult> GetCategory()
        {
            var category = await db.Category.Select(x=>new {
                x.categoryDesc,
                x.CategoryId,
                x.categoryImage,
                x.categoryName,
                x.categoryParent
            }).ToListAsync();
            return Ok(category);
        }

I'm returning an anonymous object, the propierty categoryParent its the same object as category so its recursive; 我返回一个匿名对象,属性categoryParent其对象与category相同,因此它是递归的; when I fill the database with mock data in the Category table and call the get method, everything runs OK because I dont have any data en ProductCategory, but when I fill it(the ProductCategory table) the program crashes. 当我在类别表中用模拟数据填充数据库并调用get方法时,一切都运行正常,因为在ProductCategory中没有任何数据,但是当我将其填充(ProductCategory表)时,程序崩溃。 MY entity classes are: 我的实体类别为:

public class Category {
    public int CategoryId { set; get; }
    public string categoryName { set; get; }
    public string categoryDesc { set; get; }
    public string categoryImage { set; get; }
    public int? categoryParentId { set; get; }
    public virtual ICollection<ProductCategory> ProductCategories { set; get; }
    public virtual Category categoryParent { set; get; }
}

public class Product{
    public int ProductId { set; get; }
    public string productName { set; get; }
    public string productDesc { set; get; }
    public double productPrice { set; get; }
    public string productUrl { set; get; }
    public DateTime productPublishDate { set; get; }
    public DateTime productModifyDate { set; get; }
    public bool productStatus { set; get; }
    public int productStock { set; get; }
    public virtual ICollection<ProductCategory> ProductCategories { set; get; }    
}
public class ProductCategory : IProductCategory {
    [Required]
    [Key]
    [ForeignKey("Category")]
    [Column(Order = 1)]
    public int CategoryId { set; get; }
    [Required]
    [Key]
    [ForeignKey("Product")]
    [Column(Order = 2)]
    public int ProductId { set; get; }
    public virtual Product Product { set; get; }
    public virtual Category Category { set; get; }
}

Can you help me to fix it?, So when I return categoryParent return it recursively, Thanks 您能帮我解决它吗?,所以当我返回categoryParent时,递归返回它,谢谢

I'm guessing you might have better luck if you explicitly state how you want the information organized, and remove the virtual property 我想,如果您明确说明如何组织信息并删除虚拟属性,那么可能会比较幸运。

IQueryable<Category> category = db.Category;

var result = category.Where(w => w.categoryParentId != null)
                     .Join(category,
                           child => (int)child.categoryParentId,
                           parent => parent.CategoryId,
                           (child, parent) => new { 
                                    child.categoryDesc, 
                                    child.CategoryId, 
                                    child.categoryImage, 
                                    child.categoryName, 
                                    parent
                           }         
);

return Ok(await result.ToListAsync());

That should get you the same result as your query above, then you could remove: 那应该为您获得与上述查询相同的结果,然后可以删除:

public virtual Category categoryParent { set; get; }

Thank you very much but I found the solution: https://practiceaspnet.wordpress.com/2015/11/09/many-to-many-relationships-with-additional-fields/ 非常感谢,但我找到了解决方案: https : //practiceaspnet.wordpress.com/2015/11/09/many-to-many-relationships-with-additional-fields/

I used fluent API to resolve the navigation recursive problem I had: 我使用流畅的API解决了我的导航递归问题:

modelBuilder.Entity<Category>()
.HasMany(x => x.ProductCategories)
.WithRequired(x => x.Category)
.HasForeignKey(x=>x.CategoryId);

modelBuilder.Entity<Product>()
.HasMany(x => x.ProductCategories)
.WithRequired(x => x.Product)
.HasForeignKey(x => x.ProductId);

Basically the WithRequired method prevents a navigation property on the other side of the relationship so it stops the recursion 基本上,WithRequired方法可防止在关系的另一侧导航属性,因此它会阻止递归

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

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