Casting a linq query to ObservableCollection

c#, linq, observablecollection, wpf

Solution

The type you select is `SourceItem` therefore you should use:

new ObservableCollection<SourceItem>(query.ToList());

Problem

This is my code that I am trying to bind a datagrid to: ``` var query = (from s in entity.Sources where s.CorporationId == corporationId select new SourceItem { CorporationId = s.CorporationId, Description=s.Description, IsActive = s.IsActive, Name=s.Name, SourceId=s.SourceId, TokenId=s.TokenId }); var x = new ObservableCollection<Source>(query); ``` And this is my SourceItetm class: ``` private void SourceDataGrid_AddingNewItem(object sender, System.Windows.Controls.AddingNewItemEventArgs e) { var sources = new Source(); sources.CorporationId = _corporationId; sources.Description = string.Empty; sources.IsActive = true; sources.Name = string.Empty; sources.SourceId = Guid.NewGuid(); sources.TokenId = Guid.NewGuid(); e.NewItem = sources; } public class SourceItem { private Guid _corporationId1; private string _description; private bool _isActive; private string _name; private Guid _sourceId; private Guid _tokenId; public Guid CorporationId { set { _corporationId1 = value; onPropertyChanged(this, "CorporationId"); } get { return _corporationId1; } } public string Description { set { _description = value; onPropertyChanged(this, "Description"); } get { return _description; } } public bool IsActive { set { _isActive = value; onPropertyChanged(this, "IsActive"); } get { return _isActive; } } public string Name { set { _name = value; onPropertyChanged(this, "NAme"); } get { return _name; } } public Guid SourceId { set { _sourceId = value; onPropertyChanged(this, "SourceId"); } get { return _sourceId; } } public Guid TokenId { set { _tokenId = value; onPropertyChanged(this, "TokenId"); } get { return _tokenId; } } // Declare the PropertyChanged event public event PropertyChangedEventHandler PropertyChanged; // OnPropertyChanged will raise the PropertyChanged event passing the // source property that is being updated. private void onPropertyChanged(object sender, string propertyName) { if (PropertyChanged != null) { PropertyChanged(sender, new PropertyChangedEventArgs(propertyName)); } } } } ``` I'm having problems getting the binding right. This line in particular: ``` var x = new ObservableCollection<Source>(query); ``` It is telling me that it cannot resolve constructor. Am I doing the binding right?

Original source