我使用列表框显示数据库中表的内容。每个列表框项目都使用设置为友好名称的 Text 属性和设置为唯一 ID 列的 Value 属性进行填充。数据库结构可能类似于以下内容:
CREATE TABLE GENERIC { FRIENDLY_NAME TEXT, ID INT }
我尝试了近一个小时使用 LINQ 将列表框的项目转换为 int[],但最终失败了。区分选定的项目和未选定的项目也很重要。这是我最终写的:
System.Collections.Generic.LinkedList<int>
selected = new LinkedList<int>(),
notSelected = new LinkedList<int>();
foreach (ListItem item in PhotoGalleryEdit_PhotoShoots.Items)
{
if (item.Selected)
selected.AddFirst(Convert.ToInt32(item.Value));
else
notSelected.AddFirst(Convert.ToInt32(item.Value));
}
int []arraySelected = selected.ToArray();
int []arrayNotSelected = notSelected.ToArray();
任何人都可以展示这是如何在 LINQ 中完成的吗?
(我用 C# 编写所有代码,但任何用 VB 编写的答案都非常受欢迎)
根据您的描述,我能想到的最不混乱的是:
var qry = from ListItem item in listbox.Items
select new {item.Selected, Value = Convert.ToInt32(item.Value)};
int[] arrSelected=qry.Where(x=>x.Selected).Select(x=>x.Value).ToArray();
int[] arrNotSelected=qry.Where(x=>!x.Selected).Select(x => x.Value).ToArray();
由于您使用的是 AddFirst,因此您可能还需要.Reverse()
在那里某处 - 或使用Array.Reverse()
然后。
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)