我是非常新的LINQ,我在這裏有一個問題。如何在LINQ中使用可空的內置變量?
我有這個非常簡單的類這裏演示的目的:
public class CurrencyPair
{
public string? cultureCode;
public string? currencyName;
public CurrencyPair(string iCultureCode, string iCurrencyName)
{
cultureCode = iCultureCode;
currencyName = iCurrencyName;
}
public CurrencyPair()
{
cultureCode = "";
currencyName = "";
}
}
然後,我有上述類實例的列表:
static List<CurrencyPair> currencyPairs;
現在我試圖做到這一點:
public List<string> GetCurrencyNames()
{
return (from c in currencyPairs select c.currencyName).Distinct().ToList();
}
但是我得到這個錯誤:
The type 'string' must be a non-nullable value type in order to use it as parameter 'T' in the generic type or method 'System.Nullable<T>'
如果我在類定義中刪除了和currencyName
的?
,則此錯誤消失。
那麼如何在LINQ查詢中使用可空字符串?
是的,對不起,這是我的錯誤。我想我忘記'字符串'實際上確實支持'null'值。不管怎麼說,多謝拉。 – Ahmad 2013-04-12 09:53:31