c# - 将 SQL 转换为 Linq left join with null

标签 c# .net sql linq

我如何正确地将此 SQL 转换为 linq

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

我试过了,但是不行

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();

谢谢

最佳答案

根据 this question,您想要使用 .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;

这是一个包含一些模拟数据对象的完整的工作示例:

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);
        }
    }
}

关于c# - 将 SQL 转换为 Linq left join with null,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9171063/

相关文章:

c# - 需要正则表达式来替换仅由字母或数字包围的所有符号

c# - C# 是否包括有限状态机?

php - MySql 语法错误 - 列的名称有数字

sql - 定义可延迟约束的优缺点是什么

c# - 简单使用 RSACryptoServiceProvider KeyPassword 失败

c# - 使用 linq 选择不同的

.NET 4 - Windows 窗体应用程序位置

c# - 使用反射(+hack)在运行时读取方法值?

php - 将选中的单选按钮提交到数据库

c# - 如何在 IIS 10 上使用 C# Razor 获取 Windows 用户域/用户名?