asp.net readxml使用linq sql [英] asp.net readxml using linq sql
问题描述
我正在尝试使用Readxml解析大量的xml文件来创建数据集,然后使用linq sql查询生成的数据集以获取我需要的数据。这很好用,除了一些文件有额外的表。我使用的开发网络没有连接到互联网,所以我不能分享我的实际代码。
我想编写类似的代码:
Var q1 =
(来自ds.Tables [table1]中的d1。AsEnumerable()
在ds.Tables [table2]中加入p。在d1.field上的AsEnumerable()< int>( 'group_id')等于p.field< int>('group_id')
如果ds.Tables.contains(table3)
{
加入d3in ds.Tables [table3]。p.field上的AsEnumerable()< int>('group_id')等于ds.field< int>('group_id')
} ......
关于如何做到这一点的任何想法?
I am trying to parse through a large number of xml files using Readxml to create a dataset, and then query the resulting dataset using linq sql to get the data I need. This works great except some of the files have extra tables. The development network I use is not connected to internet so I cannot share my actual code.
I would like to code something like:
Var q1 =
(from d1 in ds.Tables["table1"].AsEnumerable()
Join p in ds.Tables["table2"].AsEnumerable() on d1.field<int>(‘group_id’) equals on p.field<int>(‘group_id’)
If ds.Tables.contains("table3")
{
Join d3in ds.Tables["table3"].AsEnumerable() on p.field<int>(‘group_id’) equals on ds.field<int>(‘group_id’)
}……
Any ideas on how I can do this?
推荐答案
创建了一个用于执行相同操作的示例代码。假设必须在每个额外可用表的相同列上应用join子句。
created a sample code for doing the same. Assuming join clause has to be applied on same columns for each extra available table.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace JoinMoreThanTwoTablesDynamically
{
class Program
{
static void Main(string[] args)
{
//Created a List of List of Int for sample purpose.
//Same as dataTable is available in a dataset.
List<list><int>> dataset = new List<list><int>> {
new List<int>{1,2,5,6},
new List<int>{1,3,5,6},
new List<int>{4,5,6},
new List<int>{1,5}
};
//Query for joining first two tables
var q1 = from d1 in dataset[0]
join d2 in dataset[1]
on d1 equals d2
select d1;
//Retrieve how many tables are in there in your dataset
int noOfTables = dataset.Count;
//If you have more than two tables
//Below Code will run for each table from third table in your dataset.
if (noOfTables > 2)
{
for (int i = 2; i < noOfTables; i++)
{
q1 = from newd1 in q1
join d3 in dataset[i]
on newd1 equals d3
select newd1;
}
}
//Display Result of join on Console Window.
foreach (var item in q1)
{
Console.WriteLine(item.ToString());
}
}
}
}
希望这会救命 !!快乐编程!!
Hope this will help !! Happy Programming !!
这篇关于asp.net readxml使用linq sql的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!