我有对象包含对象的集合,其中每个对象也包含集合。下面,我粘贴了整个代码(控制台应用程序):
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;
namespace Console472
{
class Program
{
static void Main(string[] args)
{
var treeData = new GroupedTreeBranches()
{
DiscountGroup = new List<DiscountGroup>()
{
new DiscountGroup
{
Name = "DG1",
ID = 1
},
new DiscountGroup
{
Name = "DG2",
ID = 2
},
new DiscountGroup
{
Name = "DG3",
ID = 3
}
},
SubGroup = new List<SubGroup>()
{
new SubGroup
{
Name = "SG 1",
ID = 200,
Parent = new DiscountGroup(){ID = 1, Name = "DG1"}
},
new SubGroup
{
Name = "SG 2",
ID = 201,
Parent = new DiscountGroup(){ID = 1, Name = "DG1"}
},
new SubGroup
{
Name = "SG 3",
ID = 202,
Parent = new DiscountGroup(){ID = 2, Name = "DG2"}
},
new SubGroup
{
Name = "SG 4",
ID = 203,
Parent = new DiscountGroup(){ID = 2, Name = "DG2"}
}
},
PriceIntervals = new List<PriceIntervals>()
{
new PriceIntervals
{
Name = "Price 1",
ID = 100,
Parent = new DiscountGroup { ID = 1, Name = "DG1"}
},
new PriceIntervals
{
Name = "Price 2",
Parent = new DiscountGroup { ID = 1, Name = "DG1"}
},
new PriceIntervals
{
Name = "Price 2",
ID = 101,
Parent = new DiscountGroup { ID = 2, Name = "DG2"}
},
},
Rotation = new List<Rotation>()
{
new Rotation
{
Name = "Rot 1",
ID = 20,
Parent = new DiscountGroup(){ID = 200, Name = "SG 1"},
},
new Rotation
{
Name = "Rot 2",
ID = 21,
Parent = new SubGroup(){ID = 200, Name = "SG 1"},
},
new Rotation
{
Name = "Rot 3",
ID = 22,
Parent = new SubGroup(){ID = 201, Name = "SG 2"},
},
new Rotation
{
Name = "Rot 4",
ID = 23,
Parent = new SubGroup(){ID = 201, Name = "SG 2"},
},
}
};
var tn = new TreeNode();
foreach (var item in treeData.DiscountGroup)
{
tn.AddChild(item);
var sg = treeData.SubGroup.Where(w => w.Parent.Name == item.Name);
Each(sg, a => item.AddChild(a));
var pi = treeData.PriceIntervals.Where(w => w.Parent.Name == item.Name);
Each(pi, a => item.AddChild(a));
foreach (var item2 in item.Children.Where(w => w.GetType() == typeof(SubGroup)))
{
var rot = treeData.Rotation.Where(w => w.Parent.GetType() == typeof(SubGroup) && w.Parent.Name == item2.Name);
Each(rot, a => item2.AddChild(a));
}
if(item.Name == "SG 1")
{
var last = treeData.Rotation.Where(w => w.Name == "Rot 1").SingleOrDefault();
item.AddChild(last);
}
}
Console.ReadKey();
}
private static void Each<T>(IEnumerable<T> items, Action<T> action)
{
foreach (var item in items)
{
action(item);
}
}
}
public class TreeNode : ITreeNode
{
public ObservableCollection<ITreeNode> Children { get; set; }
public string Name { get; set; }
public ITreeNode Parent { get; set; }
public TreeNode()
{
Children = new ObservableCollection<ITreeNode>();
}
public void AddChild(ITreeNode child)
{
Children.Add(child);
}
public void RemoveChild(ITreeNode child)
{
Children.Remove(child);
}
}
public class DiscountGroup : TreeNode
{
public int ID { get; set; }
}
public class Rotation : TreeNode
{
public int ID { get; set; }
}
public class PriceIntervals : TreeNode
{
public int ID { get; set; }
}
public class SubGroup : TreeNode
{
public int ID { get; set; }
}
public class GroupedTreeBranches
{
public IEnumerable<DiscountGroup> DiscountGroup { get; set; }
public IEnumerable<Rotation> Rotation { get; set; }
public IEnumerable<PriceIntervals> PriceIntervals { get; set; }
public IEnumerable<SubGroup> SubGroup { get; set; }
}
public interface ITreeNode
{
ObservableCollection<ITreeNode> Children { get; set; }
string Name { get; set; }
ITreeNode Parent { get; set; }
void AddChild(ITreeNode child);
void RemoveChild(ITreeNode child);
}
}
如何获取折扣组名称为“ DG1”且其子代为typeof(SubGroup)的对象列表?
我正在尝试使用嵌套的 WHERE 语句,但是没有成功(收到空列表)。
* tn(对象名称)包含3个孩子,第一个包含另外4个孩子。
更新
我认为整个问题与内部的不同对象类型有关。例如,我有对象“ X”的集合,其中每个对象都包含类型为“ Y”或“ Z”的属性。
为了更好地理解,我粘贴了一些屏幕截图:
上面示例中的关键角色是称为“父母”的属性。它可以是4种不同类型的对象中的一种
在一个WHERE语句中可以有两个或更多不同对象类型时,如何使用LINQ?
!!最终更新!
我弄清楚了为什么它不起作用-我所有的类都继承自一个类。一个继承的财产是
public string Name { get; set; }
在“ Rotation”类中,我也具有隐藏此继承属性的相同属性。删除它可以解决问题