递归Linq查询

时间:2017-03-21 16:41:55

标签: c# entity-framework linq recursion

目前我正在使用WebApi和Entity Framework,所以我有3个实体:Products,Categories和ProductCategory;他们的关系是:

enter image description here

我的问题是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);
        }

我正在返回一个匿名对象,propierty categoryParent它与类别相同的对象,所以它是递归的;当我用Category表中的模拟数据填充数据库并调用get方法时,一切运行正常,因为我没有任何数据到ProductCategory,但是当我填充它(ProductCategory表)时程序崩溃。 我的实体类是:

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; }
}

你能帮我解决一下吗?所以当我返回categoryParent时递归返回它,谢谢

2 个答案:

答案 0 :(得分:0)

如果您明确说明您希望如何组织信息并删除虚拟财产,我猜你可能会有更好的运气

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());

这应该会得到与您上面的查询相同的结果,然后您可以删除:

public virtual Category categoryParent { set; get; }

答案 1 :(得分:0)

非常感谢,但我找到了解决方案:https://practiceaspnet.wordpress.com/2015/11/09/many-to-many-relationships-with-additional-fields/

我使用流畅的API来解决我的导航递归问题:

load

基本上,WithRequired方法会阻止关系另一端的导航属性,因此它会停止递归