-1

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();        
            }}
Peter Smith
  • 5,528
  • 8
  • 51
  • 77
habudaney
  • 9
  • 5

1 Answers1

0

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.

Peter Smith
  • 5,528
  • 8
  • 51
  • 77
  • firstly thanks but i didn't work when i write return infoQuery.ToList();. I edited my code. you can see – habudaney Dec 18 '21 at 08:21
  • Answer updated to allow for null return. However, still need sample data to know why the query is returning a null. – Peter Smith Dec 18 '21 at 09:31