在我的串列中,每個串列項包含 2 個屬性 ProductCategory ProductId
在我的專案中,ProductId 和 ProductCategory 都沒有 0 值
如果客戶將我發送為 ProductId = 0 產品類別 = 0
所以必須回傳串列中的所有專案
如果客戶以 ProductId = 2 ProductCategory = 0 的形式發送給我,我必須在所有 ProductCategory 中僅回傳 2 個 id 產品
要么
ProductId=0
ProductCategory=2
I must return all of the products in 2 id ProductCategory..
或 ProductId=3 ProductCategory=2
Return productId = 2 in product category 2
如何在不使用 if else 塊的情況下實作此代碼?我已經用 4 個 if/else 塊實作了這一點,但我想知道有沒有更好的方法
uj5u.com熱心網友回復:
您可以通過以下.Where陳述句實作:
狀況:
productId與 0 或product.ProductId等于匹配productId。productCategory與 0 或product.ProductCategory等于匹配productCategory。- 必須同時滿足條件 1 和 2。
var products = PRODUCT_LIST
.Where(x => (productId == 0 || x.ProductId == productId)
&& (productCategory == 0 || x.ProductCategory == productCategory))
.ToList();
轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/434045.html
上一篇:將鍵相同的串列中的字典值求和
