Bazı durumlarda LINQ sorgularını çalıştırırken if durumu ile kontrol gerekebilir. Örneğimizde DataModel adlı bir model sınıftan LINQ ile OrnekData tablosundan 3 adet bilgi çekelim.
IQueryable<DataModel> q = (from od in OrnekData where od.FirmaId != null select new DataModel { CalisanAdi = od.CalisanAdi, Id = od.Id, KullaniciAdi = od.KullaniciAdi.HasValue ? od.KullaniciAdi : null, Sifre = od.Sifre.HasValue ? od.Sifre : 0 });
public class DataModel { public string CalisanAdi { get; set; } public int? Sifre { get; set; } public string KullaniciAdi { get; set; } }
Kullanıcının kullanıcı adı ve şifresinin olup olmadığını HasValue özelliğiyle kontrol ettik eğer yoksa null ve 0 değerlerinin kullanılmasını sağladık.