如何正确地将此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();

谢谢

推荐答案

你想使用.DefaultIfEmpty,按照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;

下面是一些模拟数据对象的完整工作示例:

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

Sql相关问答推荐

在SQL中向每个子字节组添加字节行

在SQL:2003(PGQ)中,Cypher查询语言、GQL、PGQL和属性图查询的常见子集是什么?'

Oracle SQL-将结果列在单行中

PostgreSQL基于2个COLS的任意组合 Select 唯一行

了解放置时的连接

如何将`now()`作为SQL插入语句的一部分?

SQL Athena/prest判断值是否在嵌套的json数组中

WooCommerce产品的SQL查询:获取sku和产品标签

如何向 mariadb 添加外键?

在 Oracle 21c 中透视文本值

如何解释 SQL Server 中的 Foxpro 语法?

我们可以使用连接改进包含多个子查询的查询吗

Postgres更新增量之间的差异

如何将输出转换为二维格式?

oracle中多行的跨日期范围的交集

Set vs let vs 在snowflake中声明变量

每组跨行曲折?

将单行中的多个行值转换为列

postgreSQL 中的循环表

使用同一表中的数据或任何其他虚拟数据在 SQL 表中插入数据的最快方法