Linq select 新建动态多部分列表<>
Linq select new dynamic multi part List<>
我正在研究以下查询:
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 新语句展平为一般类型的 Id 对象列表。目前我必须编写一个具有两个属性的匿名对象来获得我想要的东西。有人可以帮助我将其重构为一个平面列表吗?这可行吗?
更新: 类别实体的外观:
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();
我已经在 Entity Framework 6.1.3 上对其进行了测试,它正确地为 select
行生成了一个 UNION ALL
... 然后使用了一个 CROSS APPLY
SelectMany
(???)
我正在研究以下查询:
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 新语句展平为一般类型的 Id 对象列表。目前我必须编写一个具有两个属性的匿名对象来获得我想要的东西。有人可以帮助我将其重构为一个平面列表吗?这可行吗?
更新: 类别实体的外观:
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();
我已经在 Entity Framework 6.1.3 上对其进行了测试,它正确地为 select
行生成了一个 UNION ALL
... 然后使用了一个 CROSS APPLY
SelectMany
(???)