我正在尝试对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()的第二个方法是原因。
我需要改变什么?
答案 0 :(得分:2)
标题的答案是这样的:http://clinq.codeplex.com/我从未使用它,但你可以试着看看它是不是你要找的东西。
答案 1 :(得分:0)
this.radChart.ItemsSource = (List<ObservableCollection<TransactionDetail>>)grouped;
由于
答案 2 :(得分:0)
你之前看过Telerik的这个链接吗?
http://www.telerik.com/help/silverlight/radchart-features-grouping-and-aggregation.html
和这一个
答案 3 :(得分:0)
将SeriesMapping修改为基于telerik用户指南的要求