【发布时间】:2019-06-20 09:05:48
【问题描述】:
我有以下实体方案:
class Container
{
Forest Forest;
Tree Tree;
}
class Forest
{
int Id;
string Name;
List<Trees> Trees;
}
class Tree
{
int Id;
string Name;
Forest Forest;
List<Leaf> Leafs;
}
class Leaf
{
int Id;
string Name;
Tree Tree;
}
我收集了 Forest 的集合,其中包含从数据库中读取的 Trees and Leafs。
我怎样才能做到以下几点:
根据以下规则过滤出Forests 的集合:
以 [Name] 包含一些 "filter value" 的 Forest 条目为例
或者那些有Trees [Name] 包含"filter value"
或者Leaf[Name]的人包含"filter value"。
我需要返回 Forest 的层次结构,而不是 Forest 的平面视图
我尝试展平结构以过滤条目,例如来自INNER JOIN 视图的数据库中的表
IEnumerable<Container> containers;
var groupped = forests.Select(f => new {f.Forest, f.Tree})
.GroupBy(f => f.Forest)
.ToList().Select(fs => new {Forest = fs.Key, Leafes = fs.SelectMany(g => g.Tree.Leafes) }).ToDictionary(fx => fx.Forest, fx => fx.Leafes);
var flat = new List<Tuple<Forest, Tree, Leaf>>;
foreach (var i in groupped)
{
foreach (var l in i.Value)
{
flat.Add((i.Key, l.Tree, l));
}
}
flat.Where(d => d.Item1.Name.Contains("")
|| d.Item2.Name.Contains("")
|| d.Item3.Name.Contains(""));
但实际上在这里我不能将它们组合回Forest -> Tree -> Leaf的层次结构
因此,我不想拥有List<Tuple<Forest,Tree,Leaf>> 的一些类似表格的结构,而是希望有一个普通的集合List<Forest> 并过滤掉Trees 和Leafs。
【问题讨论】:
-
您是要在数据库级别还是在应用程序级别过滤掉它?
-
为什么你的
Container类有一个Forest成员和一个Tree成员?Tree成员的意义是什么?您说“过滤出森林集合”,但您的代码没有Forests 的集合。 “过滤掉树木和树叶”是什么意思?
标签: c# .net entity-framework linq