2

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.

2
  • Why are all these Selects? Looks like you are selecting to the same entity types which come from the database, correct? Why not simply context.WebFairHall.Include(...).ThenInclude(,,,) etc.? Commented Aug 14, 2021 at 11:05
  • yeah i know i can use Include without taking any error but im taking too much data in that query so i just wanna take properties which im gonna use in the page. Commented Aug 14, 2021 at 11:09

1 Answer 1

2

You could test for null:

CompanyID = h.CompanyID,
Company = h.Company == null
    ? null
    : new Company ....
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.