Convert SQL to Linq left join with null

前端 未结 4 736
臣服心动
臣服心动 2020-12-08 09:10

How can I convert properly this SQL to linq

select  t1.ProgramID
from Program t1 LEFT JOIN ProgramLocation t2 ON  t1.ProgramID = t2.ProgramID 
where t2.Progr         


        
4条回答
  •  伪装坚强ぢ
    2020-12-08 09:30

    You want to use .DefaultIfEmpty, as per this question.

    var query = from p in Programs
                join pl in ProgramLocations
                    on p.ProgramID equals pl.ProgramID into pp
                from pl in pp.DefaultIfEmpty()
                where pl == null
                select p;
    

    Here's a full, working example with some mock data objects:

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    
    namespace LinqTest
    {
        class LinqProgram
        {
            public class Program
            {
                public int ProgramID { get; set; }
                public string ProgramName { get; set; }
            }
    
            public class ProgramLocation
            {
                public int ProgramLocationID { get; set; }
                public int ProgramID { get; set; }
                public string ProgramLocationName { get; set; }
            }
    
            public static List Programs = new List();
            public static List ProgramLocations = new List();
    
            static void Main(string[] args)
            {
                FillTestData();
    
                var query = from p in Programs
                            join pl in ProgramLocations
                                on p.ProgramID equals pl.ProgramID into pp
                            from pl in pp.DefaultIfEmpty()
                            where pl == null
                            select p;
    
                foreach (var r in query)
                {
                    Console.WriteLine("{0}: {1}", r.ProgramID, r.ProgramName);
                }
    
                Console.ReadLine();
            }
    
            private static void FillTestData()
            {
                var p = new Program()
                {
                    ProgramID = Programs.Count + 1,
                    ProgramName = "Scary Lesson"
                };
                var pl = new ProgramLocation()
                {
                    ProgramLocationID = ProgramLocations.Count + 1,
                    ProgramID = p.ProgramID,
                    ProgramLocationName = "Haunted House"
                };
                Programs.Add(p);
                ProgramLocations.Add(pl);
    
                p = new Program()
                {
                    ProgramID = Programs.Count + 1,
                    ProgramName = "Terrifying Teachings"
                };
    
                pl = new ProgramLocation()
                {
                    ProgramLocationID = ProgramLocations.Count + 1,
                    ProgramID = p.ProgramID,
                    ProgramLocationName = "Mystical Mansion"
                };
                Programs.Add(p);
                ProgramLocations.Add(pl);
    
                p = new Program()
                {
                    ProgramID = Programs.Count + 1,
                    ProgramName = "Unassociated Program"
                };
                Programs.Add(p);
            }
        }
    }
    

提交回复
热议问题