如何将我的 sql 查询转换为 linq 查询

How to convert my sql query to linq query

嗨,我写了这个 sql 查询,但我想写 linq 版本 sql版本

Select CalisanId From Calisanlar Where Lisans=0 INTERSECT (Select CalisanId From Covids) 
  

        public List<LisansCovid> GetLisansCovid()
            {
                using (SirketDBContext context = new SirketDBContext())
                {
                    var result =
        (from cal1 in context.Calisanlar
         where cal1.Lisans == 0
         select cal1.CalisanId)
        .Intersect
            (from cal2 in context.Covids
             select cal2.CalisanId);
    
     //exception              return result.ToList();        
            }}

尝试以下方法,未测试;假定数据库连接 db:

var infoQuery =
    (from cal1 in db.Calisanlar
    where cal1.Lisans == 0
    select cal1.CalisanId)
    .Intersect
        (from cal2 in db.Covids
        select cal2.CalisanId);
var result = inforQuery?.ToList() ?? 0;

或者,lambda 表达式

var infoQuery = 
    (db.Calisanlar.Where(x => x.Lisans == 0).Select(x => x.CalisanId))
    .Instersect(db.Covids.Select(x => x.CalisanId))

更新 假设 CalisanIDinteger 检查 null return 和 return 如果找到则为零。同样,未经测试。