Note that there are some explanatory texts on larger screens.

plurals
  1. POLINQ Anonymous Type to ObservableCollection in Custom Class
    primarykey
    data
    text
    <p>I am struggling with converting a LINQ statement which returns an anonymous type to an ObservableCollection with a custom class, I'm happy with the LINQ statement, and the class definition, the problem (I think) is to do with how I am implementing the IQueryable interface between my anonymous type and the class itself.</p> <pre><code>public class CatSummary : INotifyPropertyChanged { private string _catName; public string CatName { get { return _catName; } set { if (_catName != value) { _catName = value; NotifyPropertyChanged("CatName"); } } } private string _catAmount; public string CatAmount { get { return _catAmount; } set { if (_catAmount != value) { _catAmount = value; NotifyPropertyChanged("CatAmount"); } } } public event PropertyChangedEventHandler PropertyChanged; // Used to notify Silverlight that a property has changed. private void NotifyPropertyChanged(string propertyName) { if (PropertyChanged != null) { PropertyChanged(this, new PropertyChangedEventArgs(propertyName)); //MessageBox.Show("NotifyPropertyChanged: " + propertyName); } } } private void GetCategoryAmounts() { var myOC = new ObservableCollection&lt;CatSummary&gt;(); var myQuery = BoughtItemDB.BoughtItems .GroupBy(item =&gt; item.ItemCategory) .Select(g =&gt; new { _catName = g.Key, _catAmount = g.Sum(x =&gt; x.ItemAmount) }); foreach (var item in myQuery) myOC.Add(item); } </code></pre> <p>The error I am getting is on the last line and is<br> <code>"Argument 1: cannot convert from 'AnonymousType#1' to 'CatSummary'"</code></p> <p>I'm relatively new to c# and need pointing in the right direction - if anyone has any tutorials on this sort of thing that would help as well.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload