Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6910565
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T08:49:52+00:00 2026-05-27T08:49:52+00:00

I have the following classes: public class Student { public string StudentID { get;

  • 0

I have the following classes:

public class Student
{
    public string StudentID { get; set; }
    public string StudentName { get; set; }
}
public class Marks
{
    public string StudentID { get; set; }
    public string SubjectName { get; set; }
    public string Score { get; set; }
} 

Populating the collection:

Collection<Student> objStudentCollection = new Collection<Student>();
Student student = new Student();
student.StudentID = "104517";
student.StudentName = "John";
objStudentCollection.Add(student);

student = new Student();
student.StudentID = "104520";
student.StudentName = "Stella";
objStudentCollection.Add(student);

Collection<Marks> objMarkCollection = new Collection<Marks>();
Marks marks = new Marks();
marks.StudentID = "104517";
marks.SubjectName = "English";
marks.Score = "85";
objMarkCollection.Add(marks);

marks = new Marks();
marks.StudentID = "104517";
marks.SubjectName = "Science";
marks.Score = "60";
objMarkCollection.Add(marks);


marks = new Marks();
marks.StudentID = "104517";
marks.SubjectName = "Mathematics";
marks.Score = "75";
objMarkCollection.Add(marks);


marks = new Marks();
marks.StudentID = "104517";
marks.SubjectName = "Optional 1";
marks.Score = "75";
objMarkCollection.Add(marks);


marks = new Marks();
marks.StudentID = "104520";
marks.SubjectName = "French";
marks.Score = "54";
objMarkCollection.Add(marks);

marks = new Marks();
marks.StudentID = "104520";
marks.SubjectName = "Science";
marks.Score = "60";
objMarkCollection.Add(marks);


marks = new Marks();
marks.StudentID = "104520";
marks.SubjectName = "Mathematics";
marks.Score = "75";
objMarkCollection.Add(marks);


marks = new Marks();
marks.StudentID = "104520";
marks.SubjectName = "Optional 1";
marks.Score = "50";
objMarkCollection.Add(marks);

I would like to bind the above collections in GridView to display like:

StudentID | StudentName | English | French | Mathematics | Science | Optional 1 | Total

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-27T08:49:53+00:00Added an answer on May 27, 2026 at 8:49 am

    I think you need to utilise the GroupJoin method to get what you’re after.
    You could do it like this if the list of subjects if fixed and not going to change:

    var q = 
      objStudentCollection
      .GroupJoin(
        objMarkCollection,
        stu => stu.StudentID,
        mark => mark.StudentID,
        (stu, mark) =>
            new
            {
                student.StudentID,
                student.StudentName,
                English = mark.Where(m => m.SubjectName == "English").Sum(m => Convert.ToInt32(m.Score)),
                French  = mark.Where(m => m.SubjectName == "French").Sum(m => Convert.ToInt32(m.Score)),
                Mathematics  = mark.Where(m => m.SubjectName == "Mathematics").Sum(m => Convert.ToInt32(m.Score)),
                Science   = mark.Where(m => m.SubjectName == "Science").Sum(m => Convert.ToInt32(m.Score)),
                Optional1  = mark.Where(m => m.SubjectName == "Optional 1").Sum(m => Convert.ToInt32(m.Score)),
                Total = mark.Sum(m => Convert.ToInt32(m.Score)),
            })
    

    It’s not the prettiest code in the world, but it will give you an anonymous type with each row containing data you asked for. You could replace Sum with SingleOrDefault if there is only ever one mark per subject.

    var subjects = 
        objMarkCollection
        .Select(mark => mark.SubjectName)
        .Distinct()
        .Dump();
    
    var q =  
        objStudentCollection 
        .GroupJoin( 
            objMarkCollection, 
            stu => stu.StudentID, 
            mark => mark.StudentID, 
            (stu, mark) => 
                new 
                { 
                    student.StudentID, 
                    student.StudentName, 
                    Marks = 
                        from s in subjects
                        join m in mark on s equals m.SubjectName into outer
                        from o in outer.DefaultIfEmpty()
                        select new
                        {
                            SubjectName = s,
                            Score = (o == null) ? 0 : Convert.ToInt32(o.Score),
                        },
                    Total = mark.Sum(m => Convert.ToInt32(m.Score)), 
                }) 
        .Dump();
    

    This 2nd solution will create you an anonymous type with each student and a collection of marks including each subject (those the student has not take will have a 0 score).

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have 3 following classes: public class BaseProperty1{ public string Property1 {get; set;} }
I have following classes public class Person { public string FirstName { get; set;
I have the following classes: public class Person { public String FirstName { set;
We have following classes public class MyPropertyBase { public int StartOffset { get; set;
I have the following three classes: public class Student { private Integer studentId; private
I have the following classes: public class Note { public string Text { get;
I have the following two classes: public class Address { public string AddressLine1 {
I have the following classes [XmlRoot] public class AList { public List<B> ListOfBs {get;
Given the classes: public class Person { public string Name { get; set; }
I have the following classes: public class Folder{ private Set<Documents> documents; private Set<Clip> clips;

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.