han*_*asm 2 c# linq asp.net listbox asp.net-3.5
我使用ListBox在数据库中显示表的内容.每个列表框项都填充了Text属性设置为友好名称,Value属性设置为唯一ID列.数据库结构可能类似于以下内容:
CREATE TABLE GENERIC { FRIENDLY_NAME TEXT, ID INT }
Run Code Online (Sandbox Code Playgroud)
我尝试使用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();
Run Code Online (Sandbox Code Playgroud)
任何人都可以在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();
Run Code Online (Sandbox Code Playgroud)
由于您使用的是AddFirst,因此您可能还需要.Reverse()
在某处使用 - 或者Array.Reverse()
之后使用.
归档时间: |
|
查看次数: |
9902 次 |
最近记录: |