【问题标题】:How do you LINQ data into ObservableCollection with nested ObservableCollection?你如何使用嵌套的 ObservableCollection 将数据 LINQ 到 ObservableCollection 中?
【发布时间】:2011-03-01 20:15:24
【问题描述】:

从 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();

【问题讨论】:

    标签: c# linq mvvm windows-phone-7 windows-phone


    【解决方案1】:

    以下内容应该可以解决问题:

    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;
    }
    

    【讨论】:

      【解决方案2】:

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

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

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2012-12-06
        • 2011-06-05
        • 1970-01-01
        • 2016-11-11
        • 2015-07-12
        相关资源
        最近更新 更多