AVK
AVK

Reputation: 3923

Adding Items to List using Linq

I brought down my classes to be as simple as possible. Below are my classes.

public class QuestionData
{
    property QuestionID int {get; set;}
    property Question String {get; set;}
    property QuestionScore int {get; set;}
    <!---Few More Properties ----!>
}
public class QuestionInfo
{
    property QuestionID int {get; set;}
    property Question String {get; set;}
    property QuestionFinalScore int {get; set;}
    property List<QuestionData> ListAchieved {get; set;}
}

Below is my Sample Data

Question | QuestionID | QuestionScore 
    Q1   |      1     |     10
    Q1   |      1     |     20
    Q2   |      2     |     10
    Q2   |      2     |     -5

After going through Group in Linq and with below Query

List<QuestionData> FinalSet = // Filled with Data Here
IEnumerable<QuestionInfo> datapoints = from f in FinalSet
    group f by new { f.QuestionID, f.Question } into fGroup
    select new QuestionInfo()
    {
        QuestionID = fGroup.Key.QuestionID
        , Question = fGroup.Key.Question,
        , QuestionFinalScore = fGroup.Sum(g => g.QuestionScore)
        //, ListAchieved = This is where IDK what to do :(
    };

Below is my Output

Question | QuestionID | QuestionScore 
    Q1   |      1     |     30
    Q2   |      2     |     5

Now if you see My Class QuestionInfo, I need to Add All QuestionData to my Question ListAchieved property so that I can show it in a list as to how the Question QuestionFinalScore ended up what it is.

Can anyone point me as to what i need to do to add all these items to my List<QuestionData> Property grouped by QuestionID and Question

Upvotes: 2

Views: 1109

Answers (1)

haindl
haindl

Reputation: 3221

I think you want to do this:

, ListAchieved = fGroup.ToList()

This puts all the items of one group into a list.

Upvotes: 4

Related Questions