entity-framework - 在 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,我不知道为什么
解决方案
由于指定商品中没有图片HashSet
,匹配以下条件:
a.ImageCover == true && a.IsDelete == false
SingleOrDefault
返回null
图像之类的引用类型,当没有结果时IEnumerable
对于更具体的答案,我们需要数据结构/源和更多实现细节。
推荐阅读
- php - PhpOffice\PhpSpreadsheet 从 excel 错误字符集中导入 mysql
- css - CSS 媒体无法在一组设备上运行
- asp.net - 在没有 Internet 连接的 Web 服务器上启用 Azure AD SSO
- runtime-error - 在 Applescript 中处理 InDesign 文档时出错
- audio - 如何使用 ffmpeg 中的 libavcodec 将 opus 文件解码为 pcm 文件?
- python - 硒等到颜色变化
- mysql - 来自2个表的Mysql连接结果
- microservices - 使用 Azure Servicebus 和 Masstransit 将主题上发布的消息转发到队列
- python - 根据另一个熊猫数据框的值填充一个熊猫数据框的最快方法是什么?
- swift - 导航栏背景颜色未在 Xcode 中显示