2

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

1 Answer 1

4

I think you want to do this:

, ListAchieved = fGroup.ToList()

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

Sign up to request clarification or add additional context in comments.

2 Comments

@GiladGreen If I'm not totally wrong fGroup.ToList() returns a List<QuestionData> of the QuestionDatas that achieved the QuestionFinalScore in the given group. I've double-checked my code and it works fine. But it has been a pretty long day, so it could totally be that I didn't understand correctly what the ListAchived should consist of.
Sorry it was my mistake. I thought FinalSet was of type of QuestionInfo. Your solution with the simple .ToList() is what needed

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.