C# Program to Display the Student Details using Select Clause LINQ

This is a C# Program to display the student details using select clause linq.

Problem Description

This C# Program Displays the Student Details using Select Clause LINQ.

Problem Solution

Here select clause specifies the type of values that will be produced when the query is executed.

Program/Source Code

Here is source code of the C# Program to Display the Student Details using Select Clause LINQ. The C# program is successfully compiled and executed with Microsoft Visual Studio. The program output is also shown below.

/*
 *  C# Program to Display the Student Details using Select Clause LINQ
 */
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
class Programs
{
    public class Student
    {
        public string First { get; set; }
        public string Last { get; set; }
        public int ID { get; set; }
        public List<int> Marks;
        public ContactInfo GetContactInfo(Programs pg, int id)
        {
            ContactInfo allinfo =
                (from ci in pg.contactList
                 where ci.ID == id
                 select ci)
                .FirstOrDefault();
 
            return allinfo;
        }
 
        public override string ToString()
        {
            return First + "" + Last + " :  "   + ID;
        }
    }
 
    public class ContactInfo
    {
        public int ID { get; set; }
        public string Email { get; set; }
        public string Phone { get; set; }
        public override string ToString() { return Email + "," + Phone; }
    }
 
    public class ScoreInfo
    {
        public double Average { get; set; }
        public int ID { get; set; }
    }
    List<Student> students = new List<Student>()
        {
             new Student {First="Tom", Last=".S", ID=1, 
                          Marks= new List<int>() {97, 92, 81, 60}},
             new Student {First="Jerry", Last=".M", ID=2, 
                          Marks= new List<int>() {75, 84, 91, 39}},
             new Student {First="Bob", Last=".P", ID=3, 
                          Marks= new List<int>() {88, 94, 65, 91}},
             new Student {First="Mark", Last=".G", ID=4, 
                          Marks= new List<int>() {97, 89, 85, 82}},
        };
    List<ContactInfo> contactList = new List<ContactInfo>()
        {
            new ContactInfo {ID=111, Email="[email protected]", Phone="9328298765"},
            new ContactInfo {ID=112, Email="[email protected]", Phone="9876543201"},
            new ContactInfo {ID=113, Email="[email protected]", Phone="9087467653"},
            new ContactInfo {ID=114, Email="[email protected]", Phone="9870098761"}
        };
 
 
    static void Main(string[] args)
    {
        Programs pg = new Programs();
 
        IEnumerable<Student> studentQuery1 =
            from student in pg.students
            where student.ID > 1
            select student;
 
        Console.WriteLine("Query : Select range_variable");
        Console.WriteLine("Name    : ID");
        foreach (Student s in studentQuery1)
        {
            Console.WriteLine(s.ToString());
        }
        Console.ReadLine();
    }
}
Program Explanation

This C# program is used to display the student details using select clause LINQ. LINQ (Language Integrated Query) is uniform query syntax in C# and VB.NET used to save and retrieve data from different sources. Thereby eliminating the mismatch between programming languages and databases, as well as providing a single querying interface for different types of data sources.

advertisement
advertisement

Here select clause specifies the type of values that will be produced when the query is executed. Query Syntax starts with from clause and can be end with Select or GroupBy clause. Use various other operators like filtering, joining, grouping, sorting operators to construct the desired result. Implicitly typed variable – var can be used to hold the result of the LINQ query. Print the student details.

Runtime Test Cases
 
Enter the Number : 2
Enter the Exponent :3
Result : 8

Sanfoundry Global Education & Learning Series – 1000 C# Programs.

Sanfoundry Certification Contest of the Month is Live. 100+ Subjects. Participate Now!
If you wish to look at all C# Programming examples, go to 1000 C# Programs.

If you find any mistake above, kindly email to [email protected]

advertisement
advertisement
Subscribe to our Newsletters (Subject-wise). Participate in the Sanfoundry Certification contest to get free Certificate of Merit. Join our social networks below and stay updated with latest contests, videos, internships and jobs!

Youtube | Telegram | LinkedIn | Instagram | Facebook | Twitter | Pinterest
Manish Bhojasia - Founder & CTO at Sanfoundry
Manish Bhojasia, a technology veteran with 20+ years @ Cisco & Wipro, is Founder and CTO at Sanfoundry. He lives in Bangalore, and focuses on development of Linux Kernel, SAN Technologies, Advanced C, Data Structures & Alogrithms. Stay connected with him at LinkedIn.

Subscribe to his free Masterclasses at Youtube & discussions at Telegram SanfoundryClasses.