我有以下查询我玩大约有: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();
}
理想情况下,我想的是扁平化的选择新的语句为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; }
}
你想要一个包含'ParentId'和'ChildrenIds'的列表吗? –
是的,那正是我想要的。只是不知道如何重构它。 – IbrarMumtaz
'allCats'是什么类型?为什么在同一枚枚举中有'Join'? –