簡體   English   中英

如何檢查null並在linq匿名類型中分配新值以選擇預期的SQL結果

[英]How can I check null and assign new value in linq anonymous type to select expected sql result

我正在嘗試從產品ProductImage表中選擇帶有產品圖像的產品 ,其中ProductImages表包含產品 圖像 ,並且單個產品可以包含NullMany Images 我使用sql查詢完成了此操作,但無法編寫確切的linq查詢

我的產品表是

 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 

我的ProductImages表是

 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

我的SQL查詢選擇新表

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這樣做時,沒有得到如上表所示的預期結果。 我對此選擇的LINQ查詢是

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();

你為什么不參加兩個餐桌?

如果您對ProductsProductImages執行GroupJoin將會更快。 如果產品有任何圖像,請選擇名字和ID。如果沒有圖像,請選擇“ noProductImage”和“ NoId”。

var result = products.GroupJoin(productImages,  // GroupJoin products and productImages
    product => product.Id,                      // from every product take the Id
    image => image.ProductId,                   // from every image take the productId
    (product, images) => new                    // for every product with matching images make new:
    {
         ProductId = product.Id,
         Name = product.Name,
         Image = images
             .Select(image => image.Image) 
             .FirstOrDefault() ??             // select the first image name
              "noproductimage.jpg",           // or use a default if there is none

         ProductImageId = images
             .Select(image => image.ProductImageId.ToString())
             .FirstOrDefault() ??             // select the text of the first productId
             "NoId");                         // or use a default if there is none
    }

就像您的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”。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM