在 WPF 上,我构建了这个代码,我想在 datagrid 上显示每个“PID”的“Desc”之和
public class Event
{
public int PID { get; set; }
public int Desc { get; set; }
}
private List<Event> data;
public MainWindow()
{
InitializeComponent();
data = new List<Event>()
{
new Event() { PID = 1, Desc=2 },
new Event() { PID = 1, Desc=3 },
new Event() { PID = 2, Desc=4 },
new Event() { PID = 2, Desc=5 },
new Event() { PID = 3, Desc=6 }
};
var result =
from d in data
group d.Desc by d.PID into pg
select new { ID = pg.Key, SUM = pg.Sum() };
datagrid.ItemsSource = result;
}
XAML 是
<DataGrid Name="datagrid" AutoGenerateColumns="False" IsReadOnly="True">
<DataGrid.Columns>
<DataGridTextColumn Header="id" Binding="{Binding ID}" Width="*"/>
<DataGridTextColumn Header="Name" Binding="{Binding SUM}" Width="*"/>
</DataGrid.Columns>
</DataGrid>
效果很好!但这并不好,
我想要做的是从函数和绑定中返回“var 结果”,就像我对 DataGrid 所做的那样我该怎么做?
这是从IEnumerable<IGrouping<int,???>>
开始???有匿名类型....
那么我怎样才能像在 DataGrid 上那样从函数和绑定中返回它呢?
谢谢!