locked
Populate ObservableCollection<myClass> with Entity Framework Entity using Linq ? RRS feed

  • Question

  • I am populating an ObservableCollection<myCustomer> from an Entity Framework Entity and binding this collection to a datagrid.  Specifically, my Entity connection is to the Northwind database (and all the tables in Northwind.mdb).  I am populating the ObservableCollection<myCustomer> with the Customer entity (Customer table in the Northwind DB).  More specifically, I am only populating the ObservableCollection<myCustomer> with the first 3 columns of the Customer Entity (Customer Table), CustomerID, CompanyName, ContactName.

    myCustomer is a class I created which contains only 3 fields, CustomerID, CompanyName, ContactName.  This is what I am basing the ObservableCollection on.  Currently, I create a var List object from the entity using Linq and then loop through that to populate the ObservableCollection<myCustomer> collection.   This is working and the 3 columns of data display correctly in the datagrid when I run the app.

    Below is my code that I use, and my question is if there is a way to populate my ObservableCollection<myCustomer> without looping -- is there a way to eliminate the foreach loop?  Could I achieve this with Linq?  How would that go?  Note:  I want to keep the ObservableCollection<myCustomer> because of the binding properties (INotifyPropertyChanged, ...).

    public class myCustomer
    {
         public string CustomerID { get; set; }
         public string CompanyName { get; set; }
         public string ContactName { get; set; }
    }
    
    
    public class PersonViewModel : ObservableObject
    {
       NorthwindEntities ne = new NorthwindEntities();
    
       private ObservableCollection<myCustomer> _customers;
       public ObservableCollection<myCustomer> Customers
       {
          get
          {
             return _customers;
          }
          set
          {
    	_customers = value;
          }
       }
    
      public PersonViewModel()  //--constructor
      {
         Customers = new ObservableCollection<myCustomer>();
         var custLst = from cust in ne.Customers select cust;
         foreach (var cst in custLst.ToList())
         {
            myCustomer cust = new myCustomer();
            cust.CustomerID = cst.CustomerID;
            cust.CompanyName = cst.CompanyName;
            cust.ContactName = cst.ContactName;
            Customers.Add(cust);
         }
       }
         .....
    }


    Rich P

    Monday, August 7, 2017 4:55 PM

Answers

  • just fyi.......

    Customers = new ObservableCollection<myCustomer>(from cust in ne.Customers select new myCustomer { CustomerID = cust.CustomerID, CompanyName = cust.CompanyName, ContactName = cust.ContactName } );

    • Marked as answer by Rich P123 Tuesday, August 8, 2017 6:09 PM
    Tuesday, August 8, 2017 3:33 AM

All replies

  • Hi!

    Have you tried this : 

    public PersonViewModel()  //--constructor
      {
         
         List<myCustomer> custLst = ne.Customers().ToList();
         
    Customers=new ObservableCollection<myCustomer>(custLst);
       }

    ObservableCollection < T > has a constructor overload which takes IEnumerable < T >

    Hope it wil be useful.

    Please remember to mark the replies as answers if they help, this will help others who are looking for solutions to the same or similar problem

    Follow me on twitter : https://twitter.com/NordineMhoumadi


    Download my NEW FREE application MCP PREPARATION in the Windows 10 Store for preparing your Microsoft Certification or test your skills in Microsoft Technologies;

    Monday, August 7, 2017 6:33 PM
  • It's called using a Linq projection.

    Custom Projection with Data Source Type

    http://csharp-station.com/Tutorial/Linq/Lesson02

    What this is about with the Tolist, I don't know.

    foreach (varcst incustLst.ToList())

    It should just be this.

    foreach (var cst in custLst)

    Monday, August 7, 2017 6:42 PM
  • List<myCustomer> custLst = ne.Customers().ToList();

    I just tried this and received the following compile error message on this line:

    >>Cannot implicitly convert type 'System.Collections.Generic.List<Wpf_tmp.Customer>' to 'System.Collections.Generic.List<Wpf_tmp.Model.myCustomer>'

    myCustomer class contains only 3 fields while the entity Cutomers contains 11 fields.  I didn't think this was going to work.  I was thinking like Linq with a select and selecting only the 3 fields.  How to create a list from the Customers Entity and selecting only the 3 fields?  Sadly, I am not yet versed enough with Linq to try that out.


    Rich P

    Monday, August 7, 2017 6:52 PM

  • myCustomer class contains only 3 fields while the entity Cutomers contains 11 fields.  I didn't think this was going to work.  I was thinking like Linq with a select and selecting only the 3 fields.  How to create a list from the Customers Entity and selecting only the 3 fields?  Sadly, I am not yet versed enough with Linq to try that out.

    In order to select only 3 fields, you can use this LINQ code : 

     var custLst=from cust in ne.Customers       
            select new { CustomerID= cust.CustomerID , CompanyName = cust.CompanyName , ContactName = cust.ContactName }

    Hope it wil be useful.

    Please remember to mark the replies as answers if they help, this will help others who are looking for solutions to the same or similar problem

    Follow me on twitter : https://twitter.com/NordineMhoumadi


    Download my NEW FREE application MCP PREPARATION in the Windows 10 Store for preparing your Microsoft Certification or test your skills in Microsoft Technologies;

    Monday, August 7, 2017 7:33 PM
  • >>Cannot implicitly convert type 'System.Collections.Generic.List<Wpf_tmp.Customer>' to 'System.Collections.Generic.List<Wpf_tmp.Model.myCustomer>'

    You should be able to tell that the two objects Customer and myCustomer are not even the same object types. They don't have the same blueprints. So how can you do the cast, because that's what you are telling it to do?

    Class vs object vs instance.. OOP is OOP Java or .NET.

    https://alfredjava.wordpress.com/2008/07/08/class-vs-object-vs-instance/

    For some reason, you can't read the link that was provided to you to understand what a Linq projection or the 'shaping of the object' outputted by a Linq query is about where you tell Linq what properties of the object being projected from to the properties of the object being projected to (the mapping of the properties between the two objects).  It doesn't matter if it's a concrete object being used like myCustomer or an anonymous objects being outputted by the Linq projection.   

    Monday, August 7, 2017 8:06 PM
  • I came up with the following linq statement which eliminates the foreach loop

    var custLst = from cust in ne.Customers
                  select new myCustomer { CustomerID = cust.CustomerID, CompanyName = cust.CompanyName, ContactName = cust.ContactName };
     
    Customers = new ObservableCollection<myCustomer>(custLst);

    And the datagrid is successfully populated with this Customers list.


    Rich P

    Tuesday, August 8, 2017 3:05 AM
  • just fyi.......

    Customers = new ObservableCollection<myCustomer>(from cust in ne.Customers select new myCustomer { CustomerID = cust.CustomerID, CompanyName = cust.CompanyName, ContactName = cust.ContactName } );

    • Marked as answer by Rich P123 Tuesday, August 8, 2017 6:09 PM
    Tuesday, August 8, 2017 3:33 AM