【问题标题】:LINQ Anonymous Type to ObservableCollection in Custom Class自定义类中的 LINQ 匿名类型到 ObservableCollection
【发布时间】:2012-04-17 04:22:33
【问题描述】:

我正在努力将返回匿名类型的 LINQ 语句转换为具有自定义类的 ObservableCollection,我对 LINQ 语句和类定义感到满意,问题(我认为)与我的方式有关我正在实现我的匿名类型和类本身之间的 IQueryable 接口。

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<CatSummary>();


    var myQuery = BoughtItemDB.BoughtItems
                        .GroupBy(item => item.ItemCategory)
                        .Select(g => new 
                        { 
                            _catName = g.Key, 
                            _catAmount = g.Sum(x => x.ItemAmount)
                        });

    foreach (var item in myQuery) myOC.Add(item);
}

我得到的错误在最后一行,是
"Argument 1: cannot convert from 'AnonymousType#1' to 'CatSummary'"

我对 c# 比较陌生,需要指出正确的方向 - 如果有人有任何关于这类事情的教程也会有所帮助。

【问题讨论】:

    标签: c# wpf linq observablecollection


    【解决方案1】:

    这是因为您创建的匿名对象与CatSummary 没有类型关系。如果您想将这些项目添加到您的 ObservableCollection 中,您需要像这样构造一个 CatSummary

    BoughtItemDB.BoughtItems.GroupBy(item => item.Category)
           .Select(x => new CatSummary
           {
               CatName = x.Key,
               CatAmount = x.Sum(amt => amt.ItemAmount)
           });
    

    这样,您的查询将创建一个IEnumerable&lt;CatSummary&gt; 而不是IEnumerable&lt;a'&gt;。与其他语言及其鸭子类型不同,仅仅因为您新创建的匿名对象具有 CatName 和 CatAmount 属性并不意味着它可以代表实际类型。

    【讨论】:

    • 这项工作做得很好,单步执行代码我就知道为什么会这样了。非常感谢。
    • 我现在正在为 WPF 绑定而苦苦挣扎,如何将 XAML 对象绑定到我的自定义类?我遇到的错误是“BindingExpression path error”...“property not found on”?
    【解决方案2】:

    您可以选择带有new CatSummary(... 的CatSummary 实例,而不是选择带有new { ... 的匿名类型(或者您可以使用任何其他构建CatSummary 实例的方法)。

    【讨论】:

      【解决方案3】:

      试试这个:

       foreach (var item in myQuery) 
       {
           // You will need to create a new constructor
           var catsummary = new CatSummary(item.CatName, item.CatAmount);
           myOC.Add(catsummary); 
        }
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2011-11-07
        • 2012-04-05
        • 1970-01-01
        相关资源
        最近更新 更多