C# VB.net Linq数据表查询
我有一个数据集,其中包含如下列和数据:C# VB.net Linq数据表查询,c#,vb.net,linq,datatables,C#,Vb.net,Linq,Datatables,我有一个数据集,其中包含如下列和数据: 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”的字符串变量来选择一个“\u Id”列,并只返回与“name”列中
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”的字符串变量来选择一个“\u 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>
日本\ship\battleship\JSB018\u Yamato\u 1944\JSB018\u Yamato\u 1944\u bow\u ports.model
MP_JM025JSB018Bow_0_0
日本\misc\JM025JSB018Bow\JM025JSB018Bow.model
MP_JM026JSB018Bow_0_0
日本\misc\JM026JSB018Bow\JM026JSB018Bow.model
MP_JM029JSB018Bow_0_0
日本\misc\JM029JSB018Bow\JM029JSB018Bow.model
MP_JM033JSB018Bow_0_0
日本\misc\JM033JSB018Bow\JM033JSB018Bow.model
MP_JM109_甲板_舱口_11
日本\misc\JM109\JM109.model
MP_JM310_菊花_满
日本\misc\JM310\JM310.model
MP_CM086JSB018Bow_0_0
common\misc\CM086JSB018Bow\CM086JSB018Bow.model
MP_CM087JSB018Bow_0_0
common\misc\CM087JSB018Bow\CM087JSB018Bow.model
HP_火灾_燃烧_1
未知项目
我在这些数据中有很多条目,所以如果有比ling更快的方法,那就太酷了。
谢谢你的帮助
我应该补充一点。。如果列的值为零,则它是该节的一部分。另一方面,它是空的。试试这个。与使用字典相比,您可以使用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"];
}
}
}
使用系统;
使用System.Collections.Generic;
使用System.Linq;
使用系统文本;
使用系统数据;
命名空间控制台应用程序1
{
班级计划
{
静态void Main(字符串[]参数)
{
DataTable dt=新的DataTable();
添加(“名称”,类型(字符串));
添加(“路径”,类型(字符串));
添加(“bow_ID”,typeof(int));
dt.Columns.Add(“middfront_ID”,typeof(int));
dt.Columns.Add(“midback_ID”,typeof(int));
添加(“stern_ID”,类型(int));
Add(新对象[]{“gun1”,“guns\\guns\u 1”,0});
Add(新对象[]{“gun2”,“guns\\guns_2”,0});
Add(新对象[]{“gun3”,“guns\\guns\u 1”,0});
Add(新对象[]{“gun4”,“guns\\guns\u 2”,0});
列表筛选器=dt.AsEnumerable()
.其中(x=>x.Field(“路径”)==“枪\\枪1”)
.ToList();
//使用字典
Dictionary dict=dt.AsEnumerable()
.GroupBy(x=>x.Field(“路径”),y=>y)
.ToDictionary(x=>x.Key,y=>y.ToList());
列出枪炮1=dict[“枪炮\\枪炮2”];
}
}
}
请显示您的尝试。你用的是什么数据库?您使用什么对象来提取数据-ADO.Net
,实体框架
,等等…VB VS2013 win 7应用程序。我的代码没用。。让我更新这个问题。哇,没回答我的任何问题做得很好-这是你的问题得到答案的必要条件。显示您的代码表明您的代码不是填鸭式代码,并且已经尝试过了。让我尝试一下。。我在这里没什么进展。嘿。。谢谢我将其转换为VB并更改了第一行。。如果我理解这一点,我实际上需要从匹配输入sting,name_uu和列'name'中获取路径。。但是这行挂起Dim filter作为List(Of DataRow)=t.AsEnumerable().Where(Function(x)x.Field(Of String)(“name”)=name.ToList()您将帖子标记为Vb.net和c,所以我不确定您真正需要哪种语言。您正确地转换了代码。没有理由把它挂起来。您有数据集还是数据表?如果是数据集,则是ds.表(0)。这非常有效。这是系统。。重新开始,一切都很好。。谢谢:)