none
GroupBy result assignment problem RRS feed

  • Question

  • Hi,

    I have 3 properties:

    public List<SpectralData> ChannelData { get; set; } public List<SpectralData> ChannelDataSeries1 { get; set; }

    public List<SpectralData> ChannelDataSeries2 { get; set; }


    First property contains data which I want to group on the basis of SourceId and store the grouped values in other 2 properties

    //group by SourceID
    var groupedSeriesData = ChannelData.GroupBy(a => a.SourceID).ToList();

    As per my data this will create 2 groups. I want to assign the grouped values as follows:

    ChannelDataSeries1 = groupedSeriesData[0];
    ChannelDataSeries2 = groupedSeriesData[1];

    The above lines throwing error. I want list of group records. Can anyone help on this ??


    San


    Friday, October 19, 2012 7:48 PM

Answers

  • groupedSeriesData[int] will return an IGrouping<int, SpectralData>, which you would need to convert to a List in your example code:

    ChannelDataSeries1 = groupedSeriesData[0].ToList();
    ChannelDataSeries2 = groupedSeriesData[1].ToList();

    • Marked as answer by Sanjay Sutar Thursday, November 8, 2012 2:12 PM
    Saturday, October 20, 2012 2:37 AM
  • Hi Sanjay;

    The following query:

    var groupedSeriesData = ChannelData.GroupBy(a => a.SourceID).ToList();

    Returns a List of IGrouping<"Data Type Of SourceID", SpectralData>. For example if SpectralData is of type int then what is returned is as follows:

    List<IGrouping<int, SpectralData>>

    If you want a List<SpectralData> for the first grouping you will need to do the following.

    ChannelDataSeries1 = groupedSeriesData[0].Select( cd => cd ).ToList();
    ChannelDataSeries2 = groupedSeriesData[1].Select( cd => cd ).ToList();

      


    Fernando (MCSD)

    If a post answers your question, please click "Mark As Answer" on that post and "Mark as Helpful".

    • Marked as answer by Sanjay Sutar Thursday, November 8, 2012 2:12 PM
    Saturday, October 20, 2012 2:53 AM

All replies

  • groupedSeriesData[int] will return an IGrouping<int, SpectralData>, which you would need to convert to a List in your example code:

    ChannelDataSeries1 = groupedSeriesData[0].ToList();
    ChannelDataSeries2 = groupedSeriesData[1].ToList();

    • Marked as answer by Sanjay Sutar Thursday, November 8, 2012 2:12 PM
    Saturday, October 20, 2012 2:37 AM
  • Hi Sanjay;

    The following query:

    var groupedSeriesData = ChannelData.GroupBy(a => a.SourceID).ToList();

    Returns a List of IGrouping<"Data Type Of SourceID", SpectralData>. For example if SpectralData is of type int then what is returned is as follows:

    List<IGrouping<int, SpectralData>>

    If you want a List<SpectralData> for the first grouping you will need to do the following.

    ChannelDataSeries1 = groupedSeriesData[0].Select( cd => cd ).ToList();
    ChannelDataSeries2 = groupedSeriesData[1].Select( cd => cd ).ToList();

      


    Fernando (MCSD)

    If a post answers your question, please click "Mark As Answer" on that post and "Mark as Helpful".

    • Marked as answer by Sanjay Sutar Thursday, November 8, 2012 2:12 PM
    Saturday, October 20, 2012 2:53 AM