Linq 将MultiSelectList.SelectedValues转换为字符串[]的最小代码
这是转换为字符串[]所必需的全部内容吗Linq 将MultiSelectList.SelectedValues转换为字符串[]的最小代码,linq,asp.net-mvc-4,casting,Linq,Asp.net Mvc 4,Casting,这是转换为字符串[]所必需的全部内容吗 string[] waterfrontoptions = Model.WaterfrontOptions. SelectedValues.Cast<String>() == null ? Model.WaterfrontOptions.SelectedValues.Cast<String>().ToArray() : null; string[]waterfrontoptions=Model.waterfront
string[] waterfrontoptions = Model.WaterfrontOptions.
SelectedValues.Cast<String>() == null
? Model.WaterfrontOptions.SelectedValues.Cast<String>().ToArray() : null;
string[]waterfrontoptions=Model.waterfrontoptions。
SelectedValues.Cast()==null
? Model.WaterfrontOptions.SelectedValues.Cast().ToArray():null;
我只想在.SelectedValues属性中的值上添加一个.Contains。不,太多了
SelectedValues.Cast()
从不为空。如果SelectedValues
为null,则Cast
将引发异常。你可以做:
Model.WaterfrontOptions.SelectedValues.OfType<string>().Contains(xxxx);
我假设WaterfrontOptions
和SelectedValues
都不能为null
(因为您的原始代码具有SelectedValues.Cast
)
Model.WaterfrontOptions.SelectedValues.Select(v => v.ToString()).Contains(xxxx);