gpt4 book ai didi

mysql - 如果另一个表具有空值(序列不包含元素),则查询不显示

转载 作者:行者123 更新时间:2023-11-30 00:36:28 25 4
gpt4 key购买 nike

我的以下代码有问题。如果所有表都至少有一个值,它会显示详细信息,但如果至少一个表没有值,则不会显示任何内容。我收到消息了

sequence contains no elements.

我的代码如下:

public sneakerDetails GetSneakerDetails(int id)
{
IQueryable<sneakerDetails> query = from sneaks in _context.sneakers
from image in _context.sneakerImages
from website in _context.sneakerWebsites
// from website in _context.sneakerWebsites
where sneaks.sneaker_id == id && image.sneaker_id == website.sneaker_id && sneaks.sneaker_id == website.sneake_id
select new sneakerDetails
{
//sneaker_id = sneaks.sneaker_id,
Colorway = sneaks.Colorway,
Name = sneaks.Name,
description = sneaks.description,
imageAlternative = image.imageAlternative,
release_date = sneaks.release_date,
imageB = image.imageB,
imageF = image.imageF,
imageL = image.imageL,
imageR = image.imageR,
website = website.website,
websiteLogo = website.websiteLogo
};


return query.ToList().First();

我尝试将返回值更改为 FirstOrDefault,但是当我单击特定运动鞋时,它仅显示标题而不显示数据。

我需要为两个表编写 if 语句吗?

最佳答案

尝试使用DefaultIfEmpty():

from sneaks in _context.sneakers.DefaultIfEmpty()
from image in _context.sneakerImages.DefaultIfEmpty()
from website in _context.sneakerWebsites.DefaultIfEmpty()
// from website in _context.sneakerWebsites
where sneaks.sneaker_id == id
&& image.sneaker_id == website.sneaker_id
&& sneaks.sneaker_id == website.sneake_id
select new sneakerDetails
{
//sneaker_id = sneaks.sneaker_id,
Colorway = sneaks.Colorway,
Name = sneaks.Name,
description = sneaks.description,
imageAlternative = image.imageAlternative,
release_date = sneaks.release_date,
imageB = image.imageB,
imageF = image.imageF,
imageL = image.imageL,
imageR = image.imageR,
website = website.website,
websiteLogo = website.websiteLogo
};

关于mysql - 如果另一个表具有空值(序列不包含元素),则查询不显示,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22133132/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com