gpt4 book ai didi

c# - 将 linq 查询转换为 observablecollection

转载 作者:太空宇宙 更新时间:2023-11-03 16:48:24 25 4
gpt4 key购买 nike

我正在尝试对 linq 问题进行排序。我将数据绑定(bind)到 silverlight telerik silverlight 图,但运气不佳。我能够使图形与硬编码值一起使用,但是当我尝试将它绑定(bind)到数据库时,没有返回任何数据。

这将返回预期的数据。

  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;
}
}

像这样绑定(bind)图形

   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>

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

 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() 是原因。

我需要改变什么?

最佳答案

你好。而不是使用 .ToList()你能用 List 转换“分组”吗?

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

谢谢

关于c# - 将 linq 查询转换为 observablecollection,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5063026/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com