小编Dae*_*lus的帖子

如何忽略/删除linq查询结果C#中的非数值

我有一个linq查询,它迭代了数百个XML元素来计算工具集合中使用的特定工具的数量.

但是,工具xelement集合中的qty元素应该包含一个数字,偶尔会包含诸如"As required"之类的文本,而不是特定的数字.这显然会导致问题.

是否有一个简单的附加步骤,我可以放入这个linq查询(我对linq不好),它会忽略非数字值,还是过滤掉它们?

Linq查询是:

Dictionary<int, int> dict = listTools.Descendants("tool")
                .GroupBy(x => (int)x.Element("id"), y => (int)y.Element("qty"))
                .ToDictionary(x => x.Key, y => y.Sum());
Run Code Online (Sandbox Code Playgroud)

.net c# xml linq

7
推荐指数
1
解决办法
84
查看次数

为什么我的方法不起作用?

我有一个带有a的表单,listView通过调用该showCheckedInFiles()方法填充.当我向表单添加一个简单的按钮并按下它时,该方法完全正常,调用该方法,但是当我从其他地方调用该方法时,它将不会填充我的listview.

请帮助它让我疯狂.下面的第一个方法是从另一个类调用的,它显示在下面,我已经包含了按钮方法以供参考,正如我所说,按钮工作完美,但我需要能够调用方法而不需要单击一个按钮!!:

public void openProject(string projectname)
{
    projectName = projectname;
    string userDir = CSDBpath + projectname + "\\checkedOUT\\" + userName;
    if (!Directory.Exists(userDir)) //Does the user's directory exist, if not, create it
    {
        Directory.CreateDirectory(userDir);
    }
    showCheckedInFiles();
 }

 private void button3_Click(object sender, EventArgs e)
 {
    showCheckedInFiles();
 }
Run Code Online (Sandbox Code Playgroud)

调用上述方法:

private void buttonOpenProject_Click(object sender, EventArgs e)
{
     ListView.SelectedListViewItemCollection mySelectedItems;
     mySelectedItems = listView1.SelectedItems;
     Form1 mainform = new Form1();
     string myProject = "";

     foreach (ListViewItem item in mySelectedItems)
     {
         myProject …
Run Code Online (Sandbox Code Playgroud)

c#

0
推荐指数
1
解决办法
131
查看次数

标签 统计

c# ×2

.net ×1

linq ×1

xml ×1