Nate Pet
Nate Pet

Reputation: 46222

Initialize var value to null for strongly typed

I am trying to do something like the following:

I am not sure how to initialize teh restultCLList as I cannot set it to null.

    var resultCLlist = null;

    if (RdoStatus.SelectedValue == "Incomplete")
    {
         resultCLList = (from ms in db.ver_ServiceReport
                            join gc in db.map_Sit 
                            on ms.SiteId equals gc.SiteID
                            where gc.CompanyId == companyId
                            select new ServiceReport
                            {
                                VerificationId = ms.VerificationId,
                                SiteId = ms.SiteId,

                            }
                         ).ToList();
    }
    else
    {
         resultCLList = (from ms in db.ver_ServiceReport
                            join gc in db.map_Sites
                            on ms.SiteId equals gc.SiteID
                            where gc.CompanyId == companyId
                            select new ServiceReport
                            {
                                VerificationId = ms.VerificationId,
                                SiteId = ms.SiteId,
                                SiteName = gc.SiteName,
                                TimeStamp = ms.TimeStamp,
                                EntryDate = ms.EntryDate,
                                Supplier = ms.Supplier
                            }
                        ).ToList();

    }

Upvotes: 1

Views: 86

Answers (1)

Selman Genç
Selman Genç

Reputation: 101701

Why don't you use List<ServiceReport> instead of var ?

You can't initialize var to a null value, because null is not a type itself.You can cast it to object but it won't be safe.var is useful if you don't know the returning type of the expression on the right side, or if the type name is too long.In this case you know the type, so simply don't use var.

Upvotes: 8

Related Questions