C# Program to Demonstrate DefaultIfEmpty Method

This is a C# Program to demonstrate DefaultIfEmpty case.

Problem Description

This C# Program Demonstrates DefaultIfEmpty case.

Problem Solution

Here the default ifempty case is explained with the student database.

Program/Source Code

Here is source code of the C# Program to Demonstrate DefaultIfEmpty casee. The C# program is successfully compiled and executed with Microsoft Visual Studio. The program output is also shown below.

/*
 *  C# Program to Demonstrate DefaultIfEmpty case
 */
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
 
namespace program
{
    class student
    {
        public string Name { get; set; }
        public string stuID { get; set; }
 
    }
    class studentdetails
    {
        public string SID { get; set; }
        public string City { get; set; }
    }
 
    class Program
    {
        static void Main(string[] args)
        {
            List<student> objstudent = new List<student>{
                new student{ Name="Bob",stuID="I001"},
                new student{ Name="Vijay",stuID="I002"},
                new student{ Name="Jerry",stuID="I003"},
                new student{ Name="Tom",stuID="I004"},
                new student{ Name="Senthil",stuID="I005"},
            };
            List<studentdetails> objstudentdetails = new List<studentdetails>{
                new studentdetails{ SID="I001",City="Delhi"},
                new studentdetails{ SID="I002",City="Mumbai"},
                new studentdetails{ SID="I007",City="Chennai"},
                new studentdetails{ SID="I008",City="Pune"},
                new studentdetails{ SID="I009",City=""},
            };
            var resultDefaultIfEmpty = from stu in objstudent
                                       join studentdetails in objstudentdetails 
                                       on stu.stuID equals studentdetails.SID 
                                       into ResultEmpstudentdetails
 
             from output in ResultEmpstudentdetails.DefaultIfEmpty()
             select new
             {
                    studentName = stu.Name,
                    City = output != null ? output.City : null
             };
            Console.WriteLine(string.Join("\n", resultDefaultIfEmpty.Select(stu => 
                        " student Name = " +  stu.studentName + ", City Name = " + 
                        stu.City).ToArray<string>()));
            Console.ReadLine();
       }
    }
}
Program Explanation

This C# program is used to demonstrate DefaultIfEmpty case. Using list functions create the student name, student Id and city. Using DefaultIfEmpty() method return the elements of the specified sequence or the type parameter’s default value in a singleton collection if the sequence is empty from the database. The default ifempty case is explained with the student database.

advertisement
Runtime Test Cases
 
 student Name = Bob, City Name = Delhi
 student Name = Vijay, City Name = Mumbai
 student Name = Jerry, City Name =
 student Name = Tom, City Name =
 student Name = Senthil, City Name =

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

If you wish to look at all C# Programming examples, go to 1000 C# Programs.

advertisement
Subscribe to our Newsletters (Subject-wise). Participate in the Sanfoundry Certification 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
I’m Manish - Founder and CTO at Sanfoundry. I’ve been working in tech for over 25 years, with deep focus on Linux kernel, SAN technologies, Advanced C, Full Stack and Scalable website designs.

You can connect with me on LinkedIn, watch my Youtube Masterclasses, or join my Telegram tech discussions.

If you’re in your 20s–40s and exploring new directions in your career, I also offer mentoring. Learn more here.