如何使用嵌套的 ObservableCollection 将 LINQ 数据连接到 ObservableCollection 中?

发布于 2024-10-19 19:29:46 字数 1320 浏览 2 评论 0原文

从 xml 执行 ViewModel 的最佳方法是什么:

<Cars>
 <Car>
   <Name/>
   <Model/>
   <Parts>
     <Part>
         <PartName/>
         <PartType/>
     </Part>
     <Part>
         <PartName/>
         <PartType/>
     </Part>
   </Parts>
 </Car>
</Cars>

public class PartViewModel : INotifyPropertyChanged
{
    private string _PartName;
    private string _PartType;
    //... and proper get/seters for NotifyPropertyChanged
};

public class CarViewModel : INotifyPropertyChanged
{
    private string _Name;
    private string _Model;
    private ObservableCollection<PartViewModel> _parts;
    //... and proper get/seters for NotifyPropertyChanged
};

那么 LINQ 填充 CarViewModel 会是什么样子

 List<CarViewModel> FeedItems = (from carsXdoc in xdoc.Descendants("Cars")
                                 select new CarViewModel()
                                 {
                                     Name = carsXdoc.Element("Name").Value,
                                     Model = carsXdoc.Element("Model").Value,
// And then ? how do you fill nested observable collection with parts ?
                                 }).ToList();

What would be the best way to do ViewModel from an xml like:

<Cars>
 <Car>
   <Name/>
   <Model/>
   <Parts>
     <Part>
         <PartName/>
         <PartType/>
     </Part>
     <Part>
         <PartName/>
         <PartType/>
     </Part>
   </Parts>
 </Car>
</Cars>

would it be like

public class PartViewModel : INotifyPropertyChanged
{
    private string _PartName;
    private string _PartType;
    //... and proper get/seters for NotifyPropertyChanged
};

public class CarViewModel : INotifyPropertyChanged
{
    private string _Name;
    private string _Model;
    private ObservableCollection<PartViewModel> _parts;
    //... and proper get/seters for NotifyPropertyChanged
};

then how would LINQ look like to fill CarViewModel ?

 List<CarViewModel> FeedItems = (from carsXdoc in xdoc.Descendants("Cars")
                                 select new CarViewModel()
                                 {
                                     Name = carsXdoc.Element("Name").Value,
                                     Model = carsXdoc.Element("Model").Value,
// And then ? how do you fill nested observable collection with parts ?
                                 }).ToList();

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(2

你没皮卡萌 2024-10-26 19:29:46

像下面这样的东西应该可以解决问题:

List<CarViewModel> FeedItems = (from carsXdoc in xdoc.Descendants("Cars")
                                select new CarViewModel()
                                {
                                    Name = carsXdoc.Element("Name").Value,
                                    Model = carsXdoc.Element("Model").Value,
                                    Parts = ToObservableCollection(from part in carsXdoc.Element("Parts").Descendants("Part")
                                                                   select new PartViewModel()
                                                                   {
                                                                       PartName = part.Element("PartName").Value,
                                                                       PartType = part.Element("PartType").Value,
                                                                   })
                                }).ToList();

ToObservableCollection()方法:

ObservableCollection<T> ToObservableCollection<T>(IEnumerable<T> sequence)
{
    ObservableCollection<T> collection = new ObservableCollection<T>();
    foreach (var item in sequence)
    {
        collection.Add(item);
    }

    return collection;
}

Something like following should do the trick:

List<CarViewModel> FeedItems = (from carsXdoc in xdoc.Descendants("Cars")
                                select new CarViewModel()
                                {
                                    Name = carsXdoc.Element("Name").Value,
                                    Model = carsXdoc.Element("Model").Value,
                                    Parts = ToObservableCollection(from part in carsXdoc.Element("Parts").Descendants("Part")
                                                                   select new PartViewModel()
                                                                   {
                                                                       PartName = part.Element("PartName").Value,
                                                                       PartType = part.Element("PartType").Value,
                                                                   })
                                }).ToList();

ToObservableCollection() method:

ObservableCollection<T> ToObservableCollection<T>(IEnumerable<T> sequence)
{
    ObservableCollection<T> collection = new ObservableCollection<T>();
    foreach (var item in sequence)
    {
        collection.Add(item);
    }

    return collection;
}
梦幻的心爱 2024-10-26 19:29:46

这应该足够简单 - 只需在 select 中执行另一个嵌套 LINQ 查询即可 - 然后您可以使用采用 IEnumerable 的 ObservableCollection 构造函数。

为了保持理智,您可能需要将其分解为一个单独的函数!

This should be straight-forward enough - just do another nested LINQ query inside the select - you can then use the ObservableCollection constructor which takes and IEnumerable.

To keep your sanity you might want to break this out into a separate function!

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