考虑此模型结构:
allow
当我尝试检索特定类别的产品时,它返回null:
public class Entity : Document
{
[BsonRepresentation(BsonType.Int32)]
public int CreateUserId { get; set; }
[BsonRepresentation(BsonType.Int32)]
public int ChangeUserId { get; set; }
[BsonDateTimeOptions(Kind = DateTimeKind.Utc, Representation = BsonType.DateTime)]
public DateTime ChangeDate { get; set; }
}
public class TrackedEntity : Entity
{
public bool Deleted { get; set; }
}
public class Shop : TrackedEntity
{
public string Name { get; set; }
public string Address { get; set; }
public string MobilePhone { get; set; }
public string PhoneNumber { get; set; }
public string ShopTelegramToken { get; set; }
public string ShopAndroidId { get; set; }
public string TelegramURL { get; set; }
public string TelegramName { get; set; }
public List<Category> CategoryList { get; set; } = new List<Category>();
}
public class Category : TrackedEntity
{
[BsonRepresentation(BsonType.String)]
public string Name { get; set; }
public string Description { get; set; }
[BsonRepresentation(BsonType.String)]
public string ParentCategoryId { get; set; }
[BsonRepresentation(BsonType.String)]
public string ShopId { get; set; }
public List<Product> ProductList { get; set; } = new List<Product>();
}
public class Product : TrackedEntity
{
[BsonRepresentation(BsonType.String)]
public string Name { get; set; }
[BsonRepresentation(BsonType.String)]
public string ProductCategoryId { get; set; }
public string ImageId { get; set; }
[BsonRepresentation(BsonType.String)]
public string ShopId { get; set; }
public List<Property> PropertyList { get; set; } = new List<Property>();
}
我认为问题出在Project方法中的ElementAt方法上,因为不使用返回值就不会为空。