How to use linq with ObservableCollection?

Elad

Member
Joined
Feb 15, 2020
Messages
20
Programming Experience
1-3
Hello!
I have a problem with ObservableCollection that does not work well with linq's Where function, the ObservableCollection itself contains a class type with properties. I checked with debugger and noticed that in the whole row of the place where the place does not check even one element but returns a straight null. I have several solutions to this:
1. Enter list constructor, perform the operation with Where and convert back to ObservableCollection
2. Go through a simple loop and check exactly according to the desired situation.
But I prefer to do it in a link.
The code appears here::
 public class Package: INotifyPropertyChanged
    {
        public int PackageID { get; }
        static int count = 1000;
        public Address SenderAddress { set; get; }
        public Address DestinationAddress { set; get; }
        public Priority Priority { set; get; }
        public List<Tracking> Tracking { get; }
   
        Status _status;
        public Status Status
        { set
            {
                _status = value;
                if (PropertyChanged != null)
                {
                    PropertyChanged(this, new PropertyChangedEventArgs("Status"));
                }
            }
            get { return _status; } }
        public event PropertyChangedEventHandler PropertyChanged;
        public Package(){ this(Status.Creation,null,null)}
        public Package(Priority priority, Address senderAddress, Address destinationAdress)
        {
            PackageID = count++;
            Priority = priority;
            Tracking = new List<Tracking>();
            SenderAddress = senderAddress;
            DestinationAddress = destinationAdress;
            //Status = Status.CREATION;
            AddTracking(null);
            Console.Write("Creating " + GetType().Name+" ");

        }
 
        public override string ToString()
        {
            return "[packageID=" + PackageID + ", priority=" + Priority + ", status=" + Status + ", startTime=, senderAddress=" + SenderAddress + ", destinationAddress=" + DestinationAddress + ", ";
        }
   
        public void PrintTracking()
        {
            foreach (var item in Tracking)
            {
                System.Console.WriteLine(item);
            }
        }

    }
}
//Class implementing the interface for binding with property of Package
public class ViewPackage: INotifyPropertyChanged
 {
        public Ellipse[] EPackage { set; get; }
        public Line[] LPackage { set; get; }
        Package _pack;
        public Package Pack
        {
            set
            {
                _pack = value;
                if (PropertyChanged != null)
                {
                    PropertyChanged(this, new PropertyChangedEventArgs("Pack"));
                }
            }
            get { return _pack; }
        }
        public event PropertyChangedEventHandler PropertyChanged;
}
//MainWindow in wpf
 public partial class MainWindow : Window
    {
        BackgroundWorker worker = new BackgroundWorker();
        Visibility[] ModeVis = new Visibility[2] {Visibility.Visible, Visibility.Hidden };
        ObservableCollection<ViewPackage> ObserView;
        public MainWindow()
        {
            InitializeComponent();
            ObserView=new ObservableCollection<ViewPackage>();
            ObserView.Add(new ViewPackage() {Pack=new Package()});
            ObserView.Add(new ViewPackage() {Pack=new Package()});
            ObserView.Add(new ViewPackage() {Pack=new Package()});
            //Does not work at all!
            var v = ObserView.Where(x => x.Pack.Status == Status.HUB_STORAGE || x.Pack.Status == Status.CREATION && (x.Pack  is NonStandardPackage));
            ObservableCollection<ViewPackage> temp=v as ObservableCollection<ViewPackage>;
        }
        //....
   
    }
 
Last edited:
Of course you get null. You call Where on an ObservableCollection and then you try to cast the result as type ObservableCollection. The result is not that type so you get null. That's exactly what you should expect. If you were to call ToArray or ToList on that result then you'd see you're getting the exact results you should. If you want a new ObservableCollection then you have to create one. Given that there's a constructor that accepts an IEnumerable<T> and that's exactly what Where returns, the solution is fairly obvious:
C#:
var temp = new ObservableCollection<ViewPackage>(v);
 
Back
Top Bottom