Saturday 2 December 2017

C# Program to Demonstrate DefaultIfEmpty case


Code:

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 objstudent = new List{
                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 objstudentdetails = new List{
                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()));
            Console.ReadLine();
       }
    }
}


Output:

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 =



More C# Programs:





















100+ Best Home Decoration Ideas For Christmas Day 2019 To Make Home Beautiful

Best gifts for Christmas Day | Greeting cards for Christmas Day | Gift your children a new gift on Christmas day This Christmas d...