将领域对象的分层集合映射到 ViewModel 的分层集合
我正在尝试考虑一种有效的方法来迭代域对象的分层集合并将它们映射到相应的视图模型。
假设我有以下两种类型的域对象:
(1) 文件夹 - 该对象有两个集合 - 一个文件夹对象集合和一个项目对象集合。
(2) Item
现在,我有两个视图模型类 - 一个用于文件夹域对象,一个用于 Item 对象。我希望能够有效地迭代整个分层集合,并根据对象是文件夹还是项目,我将为相应的域对象创建一个新的视图模型类,并将该对象传递到视图模型的构造函数中。基本上,我希望最终得到分层域对象集合的分层视图模型表示。我知道我可以用一些嵌套的 foreaches 来做到这一点,但我认为有人可能知道使用扩展方法、linq 和 lambda 的方法。
感谢您的帮助。
I am trying to think of an efficient approach to iterating through a hierarchical collection of domain objects and map them to their corresponding view models.
Assume that I have the following two types of domain objects:
(1) Folder - this object has two collections - one collection of folder objects and one collection of Item objects.
(2) Item
Now, I have two view model classes - one for the Folder domain object and one for the Item object. I want to be able to efficiently iterate through my entire hierarchical collection, and based on whether the object is a Folder or an item, I will create a new view model class for the corresponding domain object and pass the object into the view model's constructor. Basically, I want to end up with a hierarchical view model representation of the hierarchical domain object collection. I know I can do this with some nested for eaches, but I thought that someone may know of a way using extension methods, linq, and lambda.
Thanks for your help.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您可以使用像这样的 LINQ 查询来联合两个集合:
如果您有一个公共基类,那么肯定最好使用它而不是“Object”
you can use a LINQ query like that to union the two collections:
if you have a common base class its for sure better to use it instead of "Object"
我猜你正在寻找这样的东西:
并且渲染可能会分别递归。
I guess you are looking for something like this:
And the rendering will probably be recursive respectively.