BerkGarip
BerkGarip

Reputation: 544

Nullable object must have a value in Select Linq

I have linq and its giving me that error. Let me show to you my linq first :

public List<WebFairHall> GetHallForFair(Guid ID,int index)
{
    return TradeTurkDBContext.WebFairHall.Where(x => x.WebFairHallIsActive == true && 
         x.WebFairField.DataGuidID == ID)
        .Select(x => new WebFairHall
        {
            WebFairHallID=x.WebFairHallID,
            HallSeatingOrders = x.HallSeatingOrders.Select(h => new HallSeatingOrder
            {
                HallSeatingOrderRowValue = h.HallSeatingOrderRowValue,
                HallSeatingOrderColumnValue = h.HallSeatingOrderColumnValue,
                CompanyID=h.CompanyID,
                Company =new Company// that's the part which is giving the exception.
                {
                    DataGuidID =  h.Company.DataGuidID,
                    CompanyName = h.Company.CompanyName,
                    FileRepos = h.Company.FileRepos.Select(f => new FileRepo
                    {
                        FileData = f.FileData
                    }).ToList()
                }
            }).ToList()
        }).OrderBy(x => x.WebFairHallID).Skip(index - 1).Take(1)
        .ToList();
}

I allready look around for that exception and i allready found out why is that happening. It's happening because Company property can be null in that HallSeatingOrders property and when i remove the part of Company in that query, the error is ending. I Check for the FileRepos for that query too and its not giving the exception so im pretty sure about it.

Let me show to you my Company.cs:

public class Company : Base
    {
        [Key]
        public int CompanyID { get; set; }

        [Required(ErrorMessage ="Required Field"), StringLength(100)]
        public string CompanyName { get; set; }

        [Required(ErrorMessage = "Required Field"), StringLength(250)]
        public string CompanyInfo { get; set; }

        /// <summary> İletişim Bilgileri
        [Required(ErrorMessage = "Required Field"), DataType(DataType.EmailAddress)]
        public string CompanyEMail { get; set; }

        public string CompanyWebSite { get; set; }
        public string CompanyAdString { get; set; }

        [Required(ErrorMessage = "Required Field"), StringLength(20)]
        public string CompanyPhone { get; set; }

        [StringLength(20)]
        public string CompanyPhone2 { get; set; }

        [StringLength(20)]
        public string CompanyMobilePhone { get; set; }

        [StringLength(20)]
        public string CompanyFax { get; set; }

        
        public string CompanyRefNo { get; set; }

        public string CompanyInstagram { get; set; }
        public string CompanyTwitter { get; set; }
        public string CompanyFacebook { get; set; }
        public string CompanyLinkedin { get; set; }
        public string CompanyYoutube { get; set; }
        public string CompanyKeyword { get; set; }
        /// </summary>

        public string CompanySlug { get; set; }
        public bool? CompanyIsCompleted { get; set; }
        public bool CompanyIsActive { get; set; }
        public int? CompanyClickCounter { get; set; }

        /// <summary>
        public int? CompanySaleAgentUser { get; set; }
        public int? WebFairHallID { get; set; }
        public bool? CompanyIsInFair { get; set; }
        public bool? CompanyFairIsFeatured { get; set; }
        public bool? CompanyProductsIsInFair { get; set; }
        public bool? CompanyIsInShopping { get; set; }
        /// </summary>

        #region /*FluentApi Objects*/
        public virtual WebFairHall WebFairHall { get; set; }
        public virtual ICollection<HallSeatingOrder> HallSeatingOrders { get; set; }
        #endregion
    }

HallSeatingOrder.cs

public class HallSeatingOrder : Base
    {
        [Key]
        [DatabaseGeneratedAttribute(DatabaseGeneratedOption.Identity)]
        public int HallSeatingOrderID { get; set; }

        [Required(ErrorMessage = "Required Field !")]
        public int WebFairHallID { get; set; }

        public int? CompanyID { get; set; }// That's the point

        [Required(ErrorMessage = "Required Field !")]
        public int HallSeatingOrderColumnValue { get; set; }

        [Required(ErrorMessage = "Required Field !")]
        public string HallSeatingOrderRowValue { get; set; }

        public bool HallSeatingOrderIsSpecial { get; set; }

        #region /*FluentApi Objects*/
        public Company Company { get; set; }That's the point
        public WebFairHall WebFairHall { get; set; }
        #endregion
    }

i dont know what to do. i tried to check properties are they null are but it didnt worked for me.

Upvotes: 2

Views: 1335

Answers (1)

Emond
Emond

Reputation: 50672

You could test for null:

CompanyID = h.CompanyID,
Company = h.Company == null
    ? null
    : new Company ....

Upvotes: 2

Related Questions