0

i want to if control in linq query, but i dont know how can i do this control.

var result = from sk in stokKartlari
                     join ue in uretimEmirleri on sk.id equals ue.UrunId
                     join ub in urunBarkodlari on ue.Id equals ub.UretimEmriId
                     select new UretimEmriJsonViewModel
                     {
                         Id = ue.Id,
                         BaslangicTarihi = string.Format("{0:yyyy-MM-dd}", ue.BaslangicTarih),
                         BitisTarihi = string.Format("{0:yyyy-MM-dd}", ue.BitisTarih),
                         StokAdi = sk.stokadi,
                         StokKartiId = ue.UrunId,
                         UretimAdet = ue.Adet,
                         UretimBasTarih = string.Format("{0:yyyy-MM-dd}", ue.UretimBasTarih),
                         UretimBitTarih = string.Format("{0:yyyy-MM-dd}", ue.UretimTamamTarih),
                         UretimSeriNo = ue.UEmirBarkod,
                         Res= //true or false
                     };

I try to if urunBarkodlari.Where(x=>x.Printed==true && x.UId==ue.Id).Count() count > 0 Res= true or false

I am doing what I want by returning the list with foreach and sending a query. Can I do this over linq without sending a query?

2 Answers2

1

urunBarkodlari.Any(x => x.Printed== true && x.UId==ue.Id).Count() > 0

You dont need "? true : false " will return true if there are any records.

You can check this post: https://stackoverflow.com/questions/3703256/linq-extension-methods-any-vs-where-vs-exists#:~:text=Any()%20returns%20true%20if,the%20IList%20back%20before%20Linq.

0

I fixed my prob;

Res = urunBarkodlari.Any(x => x.Printed== true && x.UId==ue.Id).Count() > 0

  • 1
    Not more accurate, but a little shorter: `Res= urunBarkodlari.Any(x => x.Printed && x.UId==ue.Id)` – nilsK Oct 22 '20 at 11:23