可空对象必须在Select Linq中具有值

jutyujz0  于 2023-11-14  发布在  其他
关注(0)|答案(1)|浏览(106)

我有linq,它给了我这个错误。让我先给你看看我的linq:

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();
}

字符串
我已经四处寻找这个异常,我已经找到了为什么会发生这种情况。它的发生是因为Company属性可以在HallSeatingOrders属性中为null,当我删除该查询中的Company部分时,错误就结束了。我也检查了该查询的FileRepos,它没有给出异常,所以我很确定。
让我给你看看我的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
    }


我不知道该怎么办。我试着检查属性是否为空,但它对我不起作用。

oknwwptz

oknwwptz1#

你可以测试null:

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

字符串

相关问题