I have some problems with LINQ-to-SQL and anonymous types.
I have gridview (it’s not that important, but it is Telerik’s RadGrid) which has the following datasource:
TSEntities db = new TSEntities();
int idFirma = Convert.ToInt16(Request.QueryString["idt"]);
var ticketList = (from t in db.Ticket
where t.idFirma == idFirma
select t).ToList();
gvTicketi.DataSource = from t in ticketList
where t.idFirma == idFirma
orderby t.idTicket, t.RedniBroj, t.DatumPrijave
select new { t.idTicket, t.idFirma, t.idKontakt, t.idManager, t.idNadredeniTicket, TicketNumber = t.idNadredeniTicket + "-" + t.RedniBroj, t.Biljeske, t.DatumDo, t.DatumPrijave, t.OpciPrioritet, t.Opis, t.OpisZatvoren, t.Prioritet, t.Status, t.Tip, t.VrstaPrijave, t.Zatvoren, NazivKontakta = t.Kontakt.Ime + " " + t.Kontakt.Prezime };
Everything works fine when NazivKontakta isn’t null, but when is null everything crashes with the following error: “Object reference not set to an instance of an object” which explains everything, but doesn’t help me to work it out.
What I’d like to do (if possible) is to somehow check for the null value and if it is null I’d like to set “NazivKontakta” to string value “Empty” and if it’s not null value to set it to the values from the database.
Is there a a solution for this? Any help would be appreciated!
Thank you in advance!
I suspect it’s actually this which is causing the problem:
i.e. not
NazivKontaktabeing null, butt.Kontaktbeing null. After all, if nothing’s dereferencingNazivKontaka, there shouldn’t be a problem.Try this in your query: