locked
Problems Intializing type of System.Collections.ObjectModel.ObservableCollection <T>

    Question

  •   bool  goto=  this.All.Where (t => t.Title == "TO").ToList().Count>0  " Expression cannot contain lambda expressions "

    I can't understand this exception the Variable All is   already instantiated at this point the code looks like so=>   public ObservableCollection<ViewModel.AddressViewModel> All

    {

    //Initialization code here

    }

    Monday, October 20, 2014 7:49 PM

Answers

  • Hi Magnus  actually  my code is like this ...

    bool go=this.All.Where (t => t.Title == "TO").ToList().Count>0  ;

    It will work only if "All" is an IEnumerable<T> which is a property or a field of the class in which you are declaring the go variable.

    Please share a reproducable example of your issue on OneDrive and post the link to it here if you need any futher help on this.

    Wednesday, October 22, 2014 2:48 PM
  • Hi thanks I solve the problem what I did was to apply the filter directly to my All Variable as soon as it get initialized thus because my property calls a method called FindAllLocal()

    so is like this

    public ObservableCollection<ViewModel.AddressViewModel> All

    {

    new ObservableCollection<ViewModel.AddressViewModel>(FindAllLocal().Where (t=>t.Title =="TO" ));

    }

    that whey I can simply do this  var go = All.Count > 0;

    Wednesday, October 22, 2014 5:22 PM

All replies

  • goto is a reserved keyword so you cannot name the variable "goto". However, the following code works perfectly fine assuming the AddressViewModel class has a string property called Title:

    ObservableCollection<AddressViewModel> All = new ObservableCollection<AddressViewModel>();
    bool b = All.Where(t => t.Title == "TO").ToList().Count > 0;


    this refers to the class itself which means you must declare a field or property named "All" within the same class as the bool variable, e.g:

    public ObservableCollection<ViewModel.AddressViewModel> All
    {
    get;set;
    }
    

    or

    private ObservableCollection<ViewModel.AddressViewModel> All;

    Please remember to mark helpful posts as answer and/or helpful.

    Monday, October 20, 2014 9:30 PM
  • Hi Magnus  actually  my code is like this ...

    bool go=this.All.Where (t => t.Title == "TO").ToList().Count>0  ;

    Wednesday, October 22, 2014 11:19 AM
  • Hi Magnus  actually  my code is like this ...

    bool go=this.All.Where (t => t.Title == "TO").ToList().Count>0  ;

    It will work only if "All" is an IEnumerable<T> which is a property or a field of the class in which you are declaring the go variable.

    Please share a reproducable example of your issue on OneDrive and post the link to it here if you need any futher help on this.

    Wednesday, October 22, 2014 2:48 PM
  • Hi thanks I solve the problem what I did was to apply the filter directly to my All Variable as soon as it get initialized thus because my property calls a method called FindAllLocal()

    so is like this

    public ObservableCollection<ViewModel.AddressViewModel> All

    {

    new ObservableCollection<ViewModel.AddressViewModel>(FindAllLocal().Where (t=>t.Title =="TO" ));

    }

    that whey I can simply do this  var go = All.Count > 0;

    Wednesday, October 22, 2014 5:22 PM