我有一堆下拉列表,我使用 linq 从 xml 文件中填充这些下拉列表。我尝试了以下方法来填充它们并且它以某种方式工作,唯一的问题是当一个元素丢失时,例如字幕,我在下拉列表中得到一个空白空间。此外,我没有得到每个下拉列表的不同值,而是所有值。请参阅下面的代码
var feeds = (from item in doc.Descendants("item")
select new
{
channel = (string)item.Element("channel") ?? null,
specialtoken = (string)item.Element("specialtoken") ?? null,
subchannel = (string)item.Element("subchannel") ?? null,
subtitle = (string)item.Element("subtitle") ?? null,
}).ToList().Distinct();
cboChannel.DataSource = feeds;
cboChannel.DataBind();
cboSpecialToken.DataSource = feeds;
cboSpecialToken.DataBind();
cboSubChannel.DataSource = feeds;
cboSubChannel.DataBind();
cboSubtitle.DataSource = feeds;
cboSubtitle.DataBind();
...And so on....
有没有比将每个值项拆分为单独的查询更好的方法,例如?
干杯,克里斯
var specialtoken = (from item in doc.Descendants("item")
select new
{
specialtoken = (string)item.Element("specialtoken") ?? null,
}).ToList().Distinct();
var channel= (from item in doc.Descendants("item")
select new
{
channel = (string)item.Element("channel") ?? null,
}).ToList().Distinct();
etc. etc.