将领域对象的分层集合映射到 ViewModel 的分层集合

发布于 2024-10-21 09:10:12 字数 367 浏览 2 评论 0原文

我正在尝试考虑一种有效的方法来迭代域对象的分层集合并将它们映射到相应的视图模型。

假设我有以下两种类型的域对象:

(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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

情绪 2024-10-28 09:10:12

您可以使用像这样的 LINQ 查询来联合两个集合:

public class Folder
{
}

public class Item
{
}

public IEnumerable<Object> GetChildren()
{

    Folder[] Folders = new Folder[] { };
    Item[] Items = new Item[] { };

    return ((IEnumerable<Object>)(from Folder folder in Folders 
                                  select folder))
                                  .Union<Object>(
           (IEnumerable<Object>)(from Item item in Items select item));
}

如果您有一个公共基类,那么肯定最好使用它而不是“Object”

you can use a LINQ query like that to union the two collections:

public class Folder
{
}

public class Item
{
}

public IEnumerable<Object> GetChildren()
{

    Folder[] Folders = new Folder[] { };
    Item[] Items = new Item[] { };

    return ((IEnumerable<Object>)(from Folder folder in Folders 
                                  select folder))
                                  .Union<Object>(
           (IEnumerable<Object>)(from Item item in Items select item));
}

if you have a common base class its for sure better to use it instead of "Object"

踏月而来 2024-10-28 09:10:12

我猜你正在寻找这样的东西:

public class FolderVM
{
  public string Name {get; private set;}      

  public IEnumerable<FolderVM> Folders { get; private set; }
  public IEnumerable<ItemVM> Items { get; private set; }

  public FolderVM(Folder folder)
  {
    Name = folder.Name;

    Folders = folder.ChildFolders.Select(f=> new FolderVM(f));
    Items = folder.Items.Select(i=> new ItemVM(i));
  }
}

并且渲染可能会分别递归。

I guess you are looking for something like this:

public class FolderVM
{
  public string Name {get; private set;}      

  public IEnumerable<FolderVM> Folders { get; private set; }
  public IEnumerable<ItemVM> Items { get; private set; }

  public FolderVM(Folder folder)
  {
    Name = folder.Name;

    Folders = folder.ChildFolders.Select(f=> new FolderVM(f));
    Items = folder.Items.Select(i=> new ItemVM(i));
  }
}

And the rendering will probably be recursive respectively.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文