Return IGrouping of anonymous IEnumerable to prese

2019-04-17 08:00发布

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!

1条回答
来,给爷笑一个
2楼-- · 2019-04-17 08:25

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>
查看更多
登录 后发表回答