我需要傳入在擴展方法中使用兩次的類的屬性,以檢查此屬性中的重複值。傳遞一個類的屬性作爲參數?
public static class MatchedProductsExtensionMethods
{
public static bool IdenticalValues(this List<Product> matchedProducts)
{
var itemToMatch = matchedProducts.First();
if (matchedProducts.All(p => p.PROPERTYTHATCANCHANGE == itemToMatch.PROPERTYTHATCANCHANGE))
return true;
else
return false;
}
}
所以我需要能夠在Product
類的屬性的名稱來傳遞,然後替換PROPERTYTHATCANCHANGE
。例如,Product
類中的屬性可能是ProdName
或ProdUsers
,然後我需要檢查matchedProducts
列表中是否有與這些屬性具有相同值的項目。
在此先感謝!
我正在一個錯誤:操作「==」不能被應用到類型和的操作數。 –
從技術上講,你不是傳遞一個*表達式*(這是一個'表達式'''),而是一個getter委託人 – xanatos
@DannyGoodall檢查我的編輯 - 我使用默認比較器而不是'=='運算符。 –