简体   繁体   English

使用Linq MVVM查询ObservableCollection

[英]Query ObservableCollection with Linq MVVM

I want to create a query with linq on my ObservableCollection but it doesn't really work how T tried it. 我想在我的ObservableCollection上使用linq创建一个查询,但它并没有真正起作用T如何尝试它。

I have a Model Entry which has {note, information, isActive} as parameters. 我有一个模型Entry ,其中包含{note, information, isActive}作为参数。 So I now want to simply just get all the Entries where isActive is true. 所以我现在想要简单地获取isActive为真的所有Entries I don't use it on my dataprovider (once the data gets loaded) because I need to load every entry into the program. 我不在我的dataprovider上使用它(一旦数据被加载),因为我需要将每个条目加载到程序中。

So I thought about to override the getter inside my entries ObservableCollection: 所以我想在我的条目ObservableCollection中覆盖getter:

    public ObservableCollection<Note> _entries { get; set; }
    public ObservableCollection<Note> entries
    {
        get
        {
            return new ObservableCollection<Note>(from entry in this._entries
                                                  where entry.isActive == true
                                                  select entry);
        }
        set { this._entries = value; }
    }

But as you might guess this doesn't work. 但正如你可能猜测这不起作用。

Regards 问候

Try 尝试

        get
        {
            List<Notes> list = _entries.Where(e=>e.isActive).ToList(); 
            return new ObservableCollection<Note>(list) ;
        }

Rather than editing it in the get, try updating the refinedEntries in the entries ' setter. 不要在get中编辑它,而是尝试更新entries setter中的refinedEntries My Linq statement may need work but it encapsulates what I'm trying to suggest. 我的Linq声明可能需要工作,但它包含了我想要建议的内容。

Essentially keep a copy of everything even the inactive records in entries and another collection to contain only the active records. 基本上保留所有内容的副本,即使条目中的非活动记录和另一个集合中只包含活动记录。 In this case I'm calling it refinedEntries . 在这种情况下,我称之为refinedEntries

    private ObservableCollection<Note> _entries;

    public ObservableCollection<Note> entries
    {
      get{return _entries;}
      set
      {
        _entries = value;
        RefinedEntries = new ObservableCollection(_entries.Where(e=>e.isActive).Select(e => e));
      }

     }

    public ObservableCollection<Note> refinedEntries {get;set;}

I would also suggest updating refinedEntries when CollectionChangedEvent fires. 我还建议在CollectionChangedEvent触发时更新refinedEntries In this case the only time refinedEntries is updated is when entries is set to a new instance. 在这种情况下,仅更新refinedEntries是将entries设置为新实例的时间。

When you instantiate an new collection for entries, subscribe to its CollectionChangedEvent . 为条目实例化新集合时,请订阅其CollectionChangedEvent For example if you instantiate the collection in the Model's constructor you could use the following.. 例如,如果您在Model的构造函数中实例化集合,则可以使用以下代码。

entries = new ObservableCollection<Note>();
entries.CollectionChangedEvent += new NotifyCollectionChangedEventHandler((sender,args) => 
{ 
   RefinedEntries = new ObservableCollection(_entries.Where(e=>e.isActive).Select(e => e));
  //Notify the UI that an update has been made.
  OnPropertyChanged("RefinedEntries");
});

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM