C# 如何在linq匿名类型中检查null并分配新值以选择预期的sql结果
我正在尝试从产品和产品图像表中选择具有产品图像的产品,其中产品图像表包含产品的图像,对于单个产品,它可以包含空图像或多个图像。 我使用sql查询完成了这项工作,但无法编写准确的linq查询 我的产品表是C# 如何在linq匿名类型中检查null并分配新值以选择预期的sql结果,c#,sql,sql-server,linq,C#,Sql,Sql Server,Linq,我正在尝试从产品和产品图像表中选择具有产品图像的产品,其中产品图像表包含产品的图像,对于单个产品,它可以包含空图像或多个图像。 我使用sql查询完成了这项工作,但无法编写准确的linq查询 我的产品表是 ID | Name ______________________ 1 | Masic Mouse 2 | IPhone X 3 | Thai Jeans pant 4 | Samsung Galaxy 8 5
ID | Name
______________________
1 | Masic Mouse
2 | IPhone X
3 | Thai Jeans pant
4 | Samsung Galaxy 8
5 | Apple Laptop
6 | round collar
7 | V collar t-shirt
productId | Image | ProductImageId
_________________________________________________________________
1 | 14_mm01.jpg | 1
2 | new-1.jpg | 2
3 | jeans pant.jpeg | 3
4 | 21_samsung.jpg | 4
4 | 21_samsungghf.jpg | 5
5 | images.jpg | 6
6 | 21502057.jpg | 10
我的ProductImages表是
ID | Name
______________________
1 | Masic Mouse
2 | IPhone X
3 | Thai Jeans pant
4 | Samsung Galaxy 8
5 | Apple Laptop
6 | round collar
7 | V collar t-shirt
productId | Image | ProductImageId
_________________________________________________________________
1 | 14_mm01.jpg | 1
2 | new-1.jpg | 2
3 | jeans pant.jpeg | 3
4 | 21_samsung.jpg | 4
4 | 21_samsungghf.jpg | 5
5 | images.jpg | 6
6 | 21502057.jpg | 10
MySQL用于选择新表的查询
SELECT p.id,
p.NAME,
Isnull((SELECT TOP 1 image
FROM productimages
WHERE productid = p.id), 'noproductimage.jpg') AS image,
Isnull((SELECT TOP 1 Cast(id AS VARCHAR(50))
FROM productimages
WHERE productid = p.id), 'NoId') AS ProductImageId
FROM products p
这给了我精确的结果,比如
ID | Name | Image | ProductImageId
_________________________________________________________________
1 | Masic Mouse | 14_mm01.jpg | 1
2 | IPhone X | new-1.jpg | 2
3 | Thai Jeans pant | jeans pant.jpeg | 3
4 | Samsung Galaxy 8 | 21_samsung.jpg | 4
5 | Apple Laptop | images.jpg | 6
6 | round collar | 21502057.jpg | 10
7 | V collar t-shirt | noproductimage.jpg | NoId
但是当我尝试使用linq这样做时,我并没有得到像上面的表那样的预期结果。
此选择的MyLINQ查询为
var products = from p in db.Products
select new
{
p.Id,
p.Name,
ProductImage = (from ppi in db.ProductImages where
ppi.ProductId == p.Id
select ppi.Image),
ProductImageID = (from ppi in db.ProductImages
where ppi.ProductId == p.Id
select ppi.Id)
};
此LINQ查询只返回一个结果
ID | Name | Image | ProductImageId
_________________________________________________________________
1 | Masic Mouse | 14_mm01.jpg | 1
如何获得与SQL查询返回完全相同的预期结果
var data =(from item in db.Product
join item1 in db.productImage on item.id equals item1.productId select new{
item.Id,
item.Name,
ProductImage = (item1.image==null)?”noproductimage.jpg” :item1.image,
productImageId= (item1.imageid==null) ?”NoId”: item1.imageid
}).groupBy(a=>a.productImageId).select(a=>a.firstOrDefault()).toList();
为什么不加入两个表呢?如果您要在
产品
和产品图像
上执行GroupJoin,速度会快得多。如果产品有任何图像,请选择名称和Id
如果没有图像,请选择“noProductImage”和“NoId”
该语句必须检查两次是否有任何图像,就像SQL语句一样
考虑将图像数据作为一个子类返回:
(product, images) => new // for every product with matching images make new:
{
ProductId = product.Id,
Name = product.Name,
ImageData = images.Select(image => new
{
Image = image.Image,
ProductImageId = image.ProductImageId.ToString(),
})
.FirstOrDefault() ?? // select the first image name
new // or use a default if there is none
{
Image = "noproductImage.jpg",
ProductImageId = "NoId",
},
});
最后一句话:将ProductImageId作为数字而不是文本返回不是更好吗?让数字零表示“没有Id”。MySQL和SQL Server是两种完全不同的RDBMS。发布问题时,请不要标记不相关的问题。谢谢。对此表示抱歉,并感谢@Larnu提醒我,这是一个linq查询,返回了所有具有多个产品Id的ProductImages行,其中我想要第一个或默认图像,ImageIdI尝试按照您的回答进行操作,这对我有帮助,但我无法添加“noproductimage.jpg”和“NoId”。但是对于**“noproductimage.jpg”**和“NoId”,我的代码分别使用NULL和0。感谢@leilaadd.groupby(a=>a.imageid)的帮助;在查询结束时删除重复记录