0

I am new to wpf. I am trying to create crud in wpf using entityframework. This is my model

 class User : INotifyPropertyChanged
{
    private string _id;
    private string _firstName;
    private string _lastName;


    public User()
    {
    }

    public string ID
    {
        get { return _id; }
        set
        {
            _id = value;
            NotifyOfPropertyChange("ID");
        }
    }
    public string FirstName
    {
        get { return _firstName; }
        set
        {
            _firstName = value;
            NotifyOfPropertyChange("FirstName");
        }
    }
    public string LastName
    {
        get { return _lastName; }
        set
        {
            _lastName = value;
            NotifyOfPropertyChange("LastName");
        }
    }


    #region INotifyPropertyChanged Members

    public event PropertyChangedEventHandler PropertyChanged;

    private void NotifyOfPropertyChange(string propertyName)
    {
        if (PropertyChanged != null)
        {
            PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
        }
    }
    #endregion
}

when i add ado.net data model file it creates class like this

 public partial class User
{
    public string ID { get; set; }
    public string FirstName { get; set; }
    public string LastName { get; set; }
}

so when i try to assign list Like this

 ObservableCollection<User> lst = new ObservableCollection<User>();
            lst = _entity.Users.ToList();

it gives me error

Error   1   Cannot implicitly convert type 'System.Collections.ObjectModel.ObservableCollection<TEST.User>' to 'System.Collections.Generic.List<TEST.Model.User>'   D:\my_backup\WPF\TEST\TEST\Model\PersonnelBusinessObject.cs 15  24  TEST

how can i convert it to according to my class formate.please help.

Neelam Prajapati
  • 3,764
  • 3
  • 28
  • 62
  • Try ObservableCollection lst = new ObservableCollection(_entity.Users.ToList()) – Babbillumpa Jul 20 '16 at 12:54
  • i tried this too. still getting same error :( – Neelam Prajapati Jul 20 '16 at 12:57
  • 1
    As well I understand from error message, you trying to convert different _User_ classes (`TEST.User` to `TEST.Model.User`). Compiler just don't know by default how to do this converting. You can try `_entity.Users.ToList().Select(u => new TEST.Model.User { Id = u.Id, FirstName = u.FirstName, LastName = u.LastName, })` and make `ObservableCollection` from this resulting `IEnumerable`. – Sam Jul 20 '16 at 13:12
  • the c# compiler has no idea how to convert between your two User types. write Converters between those types or fill your ViewModel properties upon initializing. – DevilSuichiro Jul 20 '16 at 13:25
  • @Sam Why didn't you write that as answer? – Clemens Jul 20 '16 at 13:48
  • 1
    @Clemens Too many times I saw how greatest SO contributors write in comments obvious answer... it's my turn ;) – Sam Jul 20 '16 at 13:58
  • This seems similar. https://stackoverflow.com/questions/17073114/how-can-i-change-an-entity-framework-icollection-to-be-an-observablecollection – William Xifaras Jul 20 '16 at 14:03

1 Answers1

4

You could map one collection type to another by the LINQ Select method:

using System.Linq;
...

var lst = new ObservableCollection<User>(
    _entity.Users.Select(u =>
        new User { ID = u.ID, FirstName = u.FirstName, LastName = u.LastName }));
Clemens
  • 123,504
  • 12
  • 155
  • 268