首页 > 解决方案 > 在 linq Entity Framework Core 中使用 Join

问题描述

Image

public partial class Image
{
    public int ImageId { get; set; }
    public string ImageName { get; set; }
    public virtual Products Products { get; set; }
}

Product

public partial class Products
{
     public Products()
     {
         Image = new HashSet<Image>();
     }

     public int ProductId { get; set; }
     public virtual ICollection<Image> Image { get; set; }
}

问题是:

 var product = _context.Products.Find(id);
 var image = product.Image.Where(a => a.ImageCover == true && a.IsDelete == false).SingleOrDefault();
 var imageName = image == null ? "no_image.jpg" : image.ImageName;

var image总是返回null,我不知道为什么

标签: entity-frameworkasp.net-core

解决方案


由于指定商品中没有图片HashSet,匹配以下条件:

a.ImageCover == true && a.IsDelete == false

SingleOrDefault返回null图像之类的引用类型,当没有结果时IEnumerable

对于更具体的答案,我们需要数据结构/源和更多实现细节。


推荐阅读