在Linq中使用where

本文关键字:where Linq | 更新日期: 2023-09-27 18:12:52

var a = from sc in context.SchoolClass
                    join bc in context.BookClass
                    on sc.ClassID equals bc.ClassID  into temp
                    from tt in temp.DefaultIfEmpty()
                    where  bc.ClassID == null && sc.SchoolYear == "2015" 
                    select sc.ClassID;

问题:在上面的代码中,"bc。ClassID == null " bc提示不存在!我知道"bc"。ClassID == null"是错误的,但我不知道如何使这正确!

在Linq中使用where

由于您在这里做left join,您将无法访问bc的属性,您可以通过tt访问属性,并且它可能包含非匹配行的null。试试这个:-

var a = from sc in context.SchoolClass
        join bc in context.BookClass
        on sc.ClassID equals bc.ClassID  into temp
        from tt in temp.DefaultIfEmpty()
        where  tt.ClassID == null && sc.SchoolYear == "2015" 
        select sc.ClassID;