Linq 选择新的动态多部件列表<>



我有以下查询我正在玩:

public void FindAllRelatedProdsByParentCat(CategoryService categoryService, string parentCatName, bool isDeleted)
{
    List<Category> allCats = categoryService.FindAll(isDeleted).ToList();
    var results = (from a in allCats.Where(x => x.Name == parentCatName)
                   join b in allCats 
                   on a.CategoryId equals b.Parentcategory into children
                   select new 
                         { 
                           ParentId = a.CategoryId, 
                           ChildrenIds = children.Select(z => z.CategoryId) 
                         }).ToList();
}

理想情况下,我想要的是将select new语句扁平化为一个泛型Id对象列表。目前,我不得不撰写一个匿名对象与两个属性得到我想要的。有人能帮我把它重构成一个平面列表吗?这可行吗?

UPDATES:类别实体是什么样子的:

public class Category : EntityBase
{
    public Category()
    {
        Products = new List<Product>();
    }
    public Guid CategoryId { get; set; }
    public string Name { get; set; }
    public Guid? Parentcategory { get; set; }
    public bool IsDeleted { get; set; }
    public Guid OrgId { get; set; }
    public virtual List<Product> Products { get; set; }
    public virtual Organization Organization { get; set; }
}

我不知道我是否理解你的意思,但是

var results = (from a in allCats.Where(x => x.Name == parentCatName)
               join b in allCats on a.CategoryId equals b.Parentcategory into children
               select new[] { a.CategoryId }.Concat(children.Select(z => z.CategoryId)))
              .SelectMany(x => x)
              .ToList();

我已经在实体框架6.1.3上测试了它,它正确地为select行生成UNION ALL…然后用CROSS APPLY代替SelectMany (??)

相关内容

  • 没有找到相关文章

最新更新