转换SQL到LINQ与空左连接SQL、LINQ

2023-09-02 01:48:25 作者:嗨翻全球

我怎么能这样的SQL转换成适当地LINQ

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.ProgramID IS NULL

我尝试,但它不能正常工作

I try that but it not working

var progy = (
             from u in db.ProgramLocations join b in db.Programs
             on u.ProgramID equals b.ProgramID into yG 
             from y1 in yG.DefaultIfEmpty() 
             where u.ProgramID == null
             where u.ProgramID == null 
             select u.ProgramID
            ).ToList();

谢谢

推荐答案

您想要使用 .DefaultIfEmpty ,按这个问题。

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<Program> Programs = new List<Program>();
        public static List<ProgramLocation> ProgramLocations = new List<ProgramLocation>();

        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);
        }
    }
}
 
精彩推荐