在下面的linq to xml查詢中,我有兩個屬性list<string>
,DefaultValues和Values。linq to xml空列表<string>元素?
如果這些元素都是空的,我想給LiteValueParameter
對象的該屬性設置爲一個新的空單:
Values = new List<string>();
相反,LINQ查詢是給我的東西艾克這樣的:
Values = new List<string>();
Values.Add("");
如果我的XML中有一個空元素,是否有任何方法可以防止將空項添加到列表中?
的Linq代碼:
//linq query
List<LiteValueParameter> valParams = new List<LiteValueParameter>();
valParams = (from c in doc.Descendants("Parameters").Descendants("Parameter")
where (LiteParameterType)Enum.Parse(typeof(LiteParameterType), c.Element("ParameterType").Value, true) == LiteParameterType.Value
select new LiteValueParameter()
{
Id = c.Attribute("Id").Value,
DataType = Type.GetType(c.Element("DataType").Value, true),
DefaultValues = c.Elements("DefaultValues").Select(element => element.Value).ToList(),
DisplayText = c.Element("DisplayText").Value,
IsRequired = Convert.ToBoolean(c.Element("IsRequired").Value),
MinCount = Convert.ToInt32(c.Element("MinCount").Value),
MaxCount = Convert.ToInt32(c.Element("MaxCount").Value),
MinValue = c.Element("MinValue").Value,
MaxValue = c.Element("MaxValue").Value,
ParameterName = c.Element("ParameterName").Value,
Values = c.Elements("Values").Select(element => element.Value).ToList(),
ParameterType = (LiteParameterType)Enum.Parse(typeof(LiteParameterType), c.Element("ParameterType").Value, true),
DisplayType = c.Element("DisplayType").Value
}).ToList();
XML代碼:
<Parameters>
<Parameter Id="PermissionList">
<ParameterType>Value</ParameterType>
<ParameterName>Permissions</ParameterName>
<DisplayType>ListBox</DisplayType>
<DisplayText>Permissions</DisplayText>
<IsRequired>true</IsRequired>
<MinValue />
<MaxValue />
<DefaultValues />
<Values />
<DataType>System.String</DataType>
<MinCount>1</MinCount>
<MaxCount>1</MaxCount>
</Parameter>
</Parameters>
您是否嘗試修改WHERE子句中的條件? – 2012-02-03 16:25:20