VB.net Linq数据表查询

本文关键字:查询 数据表 Linq net VB | 更新日期: 2023-09-27 18:06:55

我有一个数据集,它有这些列和数据,看起来像这样:

name :  path :           bow_Id :    midfront_Id :  midback_Id :  stern_id
gun1    guns'guns_1      0
gun2    guns'guns_1      0
gun3    guns'guns_1      0
gun4    guns'guns_1      0

我想使用一个名为'key'的字符串变量来选择'_Id'列之一,并只返回与'name'列中传递的字符串匹配的'path'。我试了几个例子,然后放弃了。

要填充数据集,我读取一个XML字符串,如下所示:

 dress_dataset.ReadXml(Application.StartupPath + "'WoWs_Scripts'" + s_name + ".xml")

这里是我正在加载的一些XML:

  <JSB018_Yamato_1944.xml>
<sections>
<bow>
  <model>japan'ship'battleship'JSB018_Yamato_1944'JSB018_Yamato_1944_bow_ports.model</model>
    <node>
       <name>MP_JM025JSB018Bow_0_0</name>
       <model>japan'misc'JM025JSB018Bow'JM025JSB018Bow.model</model>
    </node>
    <node>
       <name>MP_JM026JSB018Bow_0_0</name>
       <model>japan'misc'JM026JSB018Bow'JM026JSB018Bow.model</model>
    </node>
    <node>
       <name>MP_JM029JSB018Bow_0_0</name>
       <model>japan'misc'JM029JSB018Bow'JM029JSB018Bow.model</model>
    </node>
    <node>
       <name>MP_JM033JSB018Bow_0_0</name>
       <model>japan'misc'JM033JSB018Bow'JM033JSB018Bow.model</model>
    </node>
    <node>
       <name>MP_JM109_Deck_Hatch_11</name>
       <model>japan'misc'JM109'JM109.model</model>
    </node>
    <node>
       <name>MP_JM310_Chrysanthemum_full</name>
       <model>japan'misc'JM310'JM310.model</model>
    </node>
    <node>
       <name>MP_CM086JSB018Bow_0_0</name>
       <model>common'misc'CM086JSB018Bow'CM086JSB018Bow.model</model>
    </node>
    <node>
       <name>MP_CM087JSB018Bow_0_0</name>
       <model>common'misc'CM087JSB018Bow'CM087JSB018Bow.model</model>
    </node>
    <node>
       <name>HP_Fire_Burn_1</name>
       <model>UNKNOWN_ITEM</model>
    </node>
</bow>

我在这个数据中有很多条目,所以如果有比ling更快的方法来做这件事,那就太酷了。谢谢你的帮助我应该补充一下……如果列的值为0,则它属于该部分。否则,它就是空的

VB.net Linq数据表查询

试试这个。您可以使用GroupBy()将所有相同的路径分组在一起,而不是使用字典,以便您可以通过键

查找。
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
namespace ConsoleApplication1
{
    class Program
    {
        static void Main(string[] args)
        {
            DataTable dt = new DataTable();
            dt.Columns.Add("name", typeof(string));
            dt.Columns.Add("path", typeof(string));
            dt.Columns.Add("bow_ID", typeof(int));
            dt.Columns.Add("midfront_ID", typeof(int));
            dt.Columns.Add("midback_ID", typeof(int));
            dt.Columns.Add("stern_ID", typeof(int));
            dt.Rows.Add(new object[] {"gun1","guns''guns_1", 0});
            dt.Rows.Add(new object[] {"gun2","guns''guns_2", 0});
            dt.Rows.Add(new object[] {"gun3","guns''guns_1", 0});
            dt.Rows.Add(new object[] {"gun4","guns''guns_2", 0});
            List<DataRow> filter = dt.AsEnumerable()
                .Where(x => x.Field<string>("path") == "guns''guns_1" )
                .ToList();
            //use dictionary
            Dictionary<string, List<DataRow>> dict = dt.AsEnumerable()
                .GroupBy(x => x.Field<string>("path"), y => y)
                .ToDictionary(x => x.Key, y => y.ToList());
            List<DataRow> guns_1 = dict["guns''guns_2"]; 
        }
    }
}
​