Home > other >  How to convert my sql query to linq query
How to convert my sql query to linq query

Time:12-18

Hi i write this sql query but i want to write linq version sql version

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

CodePudding user response:

Try the following, not tested; assumes a database connection 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;

or, lambda expression

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

UPDATE Assuming CalisanID is an integer checks for null return and returns zero if found. Again, not tested.

  • Related