【问题标题】:Converting a linq query into an observablecollection将 linq 查询转换为 observablecollection
【发布时间】:2011-02-21 06:35:08
【问题描述】:

我正在尝试对 linq 问题进行排序。 我正在将数据绑定到 silverlight telerik silverlight 图表,但运气不佳。我可以让图形使用硬编码值,但是当我尝试将它绑定到数据库时,没有返回任何数据。

这会返回预期的数据。

  public class TranactionDetail
{
    public DateTime TransactionDate { get; set; }
    public int TransactionType { get; set; }
    public double TransactionAmount { get; set; }
    public int Month{ get; set; }
    public string MonthName { get; set; }
    public double TransactionCount { get; set; }


    public static ObservableCollection<TranactionDetail> GetTransactions(int transactionType)
    {
        ObservableCollection<TranactionDetail> data;


        if (transactionType == 1)
        {
            data = new ObservableCollection<TranactionDetail>()
                       {
                        new TranactionDetail(){TransactionAmount = 1,Month = DateTime.Now.Month,TransactionType = 1} ,
                        new TranactionDetail(){TransactionAmount = 11,Month = DateTime.Now.AddMonths(1).Month,TransactionType = 1} ,
                        new TranactionDetail(){TransactionAmount = 3,Month = DateTime.Now.AddMonths(2).Month,TransactionType = 1} ,
                        new TranactionDetail(){TransactionAmount = 1,Month = DateTime.Now.AddMonths(3).Month,TransactionType = 1} ,
                        new TranactionDetail(){TransactionAmount = 1,Month = DateTime.Now.AddMonths(4).Month,TransactionType = 1} ,
                        new TranactionDetail(){TransactionAmount = 1,Month = DateTime.Now.AddMonths(5).Month,TransactionType = 1} ,
                        new TranactionDetail(){TransactionAmount = 12,Month = DateTime.Now.AddMonths(6).Month,TransactionType = 1} ,
                        new TranactionDetail(){TransactionAmount = 1,Month = DateTime.Now.AddMonths(7).Month,TransactionType = 1} ,
                        new TranactionDetail(){TransactionAmount = 1,Month = DateTime.Now.AddMonths(8).Month,TransactionType = 1} ,
                       };

        }
        else 
        {
            data = new ObservableCollection<TranactionDetail>()
            {
                        new TranactionDetail(){TransactionAmount = 1,Month = DateTime.Now.Month,TransactionType = 2} ,
                        new TranactionDetail(){TransactionAmount = 1,Month = DateTime.Now.AddMonths(1).Month,TransactionType = 2} ,
                        new TranactionDetail(){TransactionAmount = 2,Month = DateTime.Now.AddMonths(2).Month,TransactionType = 2} ,
                        new TranactionDetail(){TransactionAmount = 5,Month = DateTime.Now.AddMonths(3).Month,TransactionType = 2} ,
                        new TranactionDetail(){TransactionAmount = 1,Month = DateTime.Now.AddMonths(4).Month,TransactionType = 2} ,
                        new TranactionDetail(){TransactionAmount = 1,Month = DateTime.Now.AddMonths(5).Month,TransactionType =2} ,
                        new TranactionDetail(){TransactionAmount = 1,Month = DateTime.Now.AddMonths(6).Month,TransactionType = 2} ,
                        new TranactionDetail(){TransactionAmount = 1,Month = DateTime.Now.AddMonths(7).Month,TransactionType = 2} ,
                        new TranactionDetail(){TransactionAmount = 7,Month = DateTime.Now.AddMonths(8).Month,TransactionType = 2} ,
            };
        }
        return data;
    }
}

像这样绑定图表

   List<ObservableCollection<TransactionDetail>> data = new List<ObservableCollection<ransactionDetail>>();
        data.Add(TransactionDetail.GetTransactions(1));
        data.Add(TransactionDetail.GetTransactions(3));         
        this.radChart.ItemsSource = data;

Xaml

<telerik:RadChart x:Name="radChart">
        <telerik:RadChart.DefaultView>
            <telerik:ChartDefaultView>
                <telerik:ChartDefaultView.ChartTitle>
                    <telerik:ChartTitle Content="Transaction Data" />
                </telerik:ChartDefaultView.ChartTitle>

            </telerik:ChartDefaultView>
        </telerik:RadChart.DefaultView>
        <telerik:RadChart.SeriesMappings>
            <telerik:SeriesMapping LegendLabel="Processed" CollectionIndex="0">
                <telerik:SeriesMapping.SeriesDefinition>
                    <telerik:LineSeriesDefinition ShowItemLabels="True" />
                </telerik:SeriesMapping.SeriesDefinition>
                <telerik:SeriesMapping.ItemMappings>
                    <telerik:ItemMapping DataPointMember="YValue" FieldName="TransactionAmount" />
                    <telerik:ItemMapping DataPointMember="XValue" FieldName="Month" />
                </telerik:SeriesMapping.ItemMappings>
            </telerik:SeriesMapping>
            <telerik:SeriesMapping LegendLabel="Redeemed" CollectionIndex="1">
                <telerik:SeriesMapping.SeriesDefinition>
                    <telerik:LineSeriesDefinition ShowItemLabels="True" />
                </telerik:SeriesMapping.SeriesDefinition>
                <telerik:SeriesMapping.ItemMappings>
                    <telerik:ItemMapping DataPointMember="YValue" FieldName="TransactionAmount" />
                    <telerik:ItemMapping DataPointMember="XValue" FieldName="Month"  />
                </telerik:SeriesMapping.ItemMappings>
            </telerik:SeriesMapping>
        </telerik:RadChart.SeriesMappings>
    </telerik:RadChart>

现在,如果我尝试将数据绑定到实际数据,例如

 var grouped = from t in TransctionData
                      group t by new
                      {
                          t.PurchaseDate.Value.Month
                      }
                          into g
                          select new TransactionDetail()
                          {
                              Month = g.Select((n => n.PurchaseDate.Value.Month)).First(),
                              TransactionAmount = g.Count()
                          };


        List<ObservableCollection<TransactionDetail>> data = new     List<ObservableCollection<TransactionDetail>>();
        this.radChart.ItemsSource = grouped.ToList();

没有返回任何东西。我已经放置了一个断点,我可以看到这是分组内的值,它们看起来确实像 TransactionDetail。

我假设使用 observablecollection 的第一种方法与仅使用 tolist() 的第二种方法是原因。

我需要改变什么?

【问题讨论】:

  • 你为什么选择 RewardTransactionDetail 类型的数据,而你的列表采用 TransactionDetail 类型.. 他们是否共享任何继承关系
  • 这里有严重问题。您没有在第二个示例中使用 data。而group.ToList() 将返回一个List&lt;RewardTransactionDetail&gt;。无法与您的第一个样本进行直接比较。
  • 嘿 Ani 对不起,我复制不正确。现已更新
  • 你们是否曾经为自己的问题创建一个简单的代码示例,或者只是复制粘贴 100 多行未知的类、函数,甚至与所提出的问题无关?

标签: c# silverlight linq


【解决方案1】:

你好。 而不是使用 .ToList() 你能用列表投射“分组”吗>

this.radChart.ItemsSource = (List<ObservableCollection<TransactionDetail>>)grouped;

谢谢

【讨论】:

  • 嗨 Robbie,我尝试了你的建议,但是我收到了这个错误 Unable to cast object of type 'WhereSelectEnumerableIterator2[System.Linq.IGrouping2[f__AnonymousType01[System.Int32],WebUI.Entry],Silverlight.TransactionDetail]' to type 'System.Collections.Generic.List1[System.Collections.ObjectModel.ObservableCollection`1 [Silverlight.TransactionDetail]]'.
  • 嘿伙计,几周前我在使用 RadChart 时遇到了同样的问题。您是否尝试过从代码中完全删除 Observable Collection 类型。我个人发现 Observable Collection 类型在绑定时很痛苦。
  • 是的,我尝试将其绑定到分组数据,但我得到的只是“无数据系列”显示
  • 原来是因为我的 xaml 期待 2 个 SeriesDefinitions 而我只提供了 1 个
【解决方案2】:

根据 Telerik 用户指南将 SeriesMapping 修改为需求

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2014-08-14
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-02-17
    • 2011-02-25
    相关资源
    最近更新 更多