返回匿名IEnumerable的IGrouping以呈现在DataGrid上 [英] Return IGrouping of anonymous IEnumerable to present on DataGrid

查看:370
本文介绍了返回匿名IEnumerable的IGrouping以呈现在DataGrid上的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在WPF上我建立这个代码
我想在datagrid上显示每个PID的Desc总和

  public class Event 
{
public int PID {get;组; }
public int Desc {get;组; }
}

私人列表<事件>数据;
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 = 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 =datagridAutoGenerateColumns =FalseIsReadOnly =True> 
< DataGrid.Columns>
< DataGridTextColumn Header =idBinding ={Binding ID}Width =*/>
< DataGridTextColumn Header =NameBinding ={Binding SUM}Width =*/>
< /DataGrid.Columns>
< / DataGrid>

工作得很好!
但这不好,



我想做的是从函数返回var result和绑定,就像我对DataGrid
如何做到这一点?



这是从 IEnumerable< IGrouping< int,???>
在???有匿名类型....



那么我可以如何在DataGrid上从函数和绑定中返回这个?



谢谢!

解决方案

您需要一个类来表示序列中的每个项目。这样你的结果不会是匿名类型的对象序列,而是一个特定类型的对象序列。

  public class Result 
{
public int Id {get; set;}
public int Sum {get;组; }
}

然后,您将定义一个如下所示的方法:

  public IEnumerable< Result> GetResults()
{
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 Result
{
Id = pg.Key ,
Sum = pg.Sum()
};

返回结果;
}

然后在您的 MainWindow 方法你会称之为这个方法。

  public MainWindow()
{
InitializeComponent();
datagrid.ItemsSource = GetResults();
}

我有假设你已经在同一个类中定义了这个方法。很可能这不是一个好习惯。因此,如果您在另一个类中定义了此方法,则必须首先创建此类的对象,然后再调用此对象的 GetResults 方法。



此外,我尝试在命名方面稍作改动。更常见的是使用骆驼案例命名,而不是对所有字母使用大写字母。话虽如此,你也可以稍微改变你的xaml代码。

 < DataGrid Name =datagridAutoGenerateColumns =FalseIsReadOnly =True> 
< DataGrid.Columns>
< DataGridTextColumn Header =idBinding ={Binding Id}Width =*/>
< DataGridTextColumn Header =NameBinding ={Binding Sum}Width =*/>
< /DataGrid.Columns>
< / DataGrid>


On WPF I build this code I want to show on datagrid the sum of "Desc" for each "PID"

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

And the XAML is

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

that work well! but this is not good,

What I want to do is return that "var result" from function and binding like I did to DataGrid How can I do this?

This is from IEnumerable<IGrouping<int,???>> on ??? there is anonymous type....

so how can I return this from function and binding like I did on DataGrid?

Thanks!

解决方案

You need a class to represent each item of your sequence. This way your result wouldn't be a sequence of objects of an anonymous type, but it would be sequence of objects of a specific type.

public class Result 
{
    public int Id { get; set;}
    public int Sum { get; set; }
}

Then you will define a method like below:

public IEnumerable<Result> GetResults()
{
    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 Result
                 { 
                     Id = pg.Key,  
                     Sum = pg.Sum()
                 };

    return result;
}

and then in your MainWindow method you will call this method.

public MainWindow()
{
    InitializeComponent();
    datagrid.ItemsSource = GetResults();
}

I have supposed that you have defined this method in the same class. It is quite possible that this is not a good practice. So if you define this method in another class, you have to create first an object of this class and later call the GetResults method of this object.

Furthermore, I attempted to make a slight change in the naming. It is more common to use camel case naming and not use capital letters for all the letters. That being said, you have also to make a slight change in your xaml code.

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

这篇关于返回匿名IEnumerable的IGrouping以呈现在DataGrid上的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆