数据表查询使用LINQ和C# [英] querying datatable using linq and c#
本文介绍了数据表查询使用LINQ和C#的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我已经看过很多网站,我不能够理解我怎么会去使用LINQ查询第一对夫妇一个数据表的行。
我也想知道有关于这一点,如果将数据从一个Excel文件来将列引用是一样的吗?例如F列在将在数据表或编号引用相同?
解决方案
的DataTable yourDataTable =新的DataTable();
VAR的结果= yourDataTable.AsEnumerable()
。取(2)//选择前两行
。选择(R =>
新
{
字段1 = r.Field< INT>(COL1)//选择列
字段2 = r.Field<串GT(COL2)
//你列的其余部分
}
);
如果您只想选择第二行则:
VAR的结果= yourDataTable.AsEnumerable()
.Skip(1)//跳过第一行
。取(1)//选择第二行
。选择(R =>
新
{
字段1 = r.Field< INT>(COL1)//选择列
字段2 = r.Field<串GT(COL2)
//你列的其余部分
}
);
编辑:
要选择所有列,而不是具体的一个,刚刚从声明中删除选择
。事情是这样的:
VAR的结果= yourDataTable.AsEnumerable()
.Skip(1)//跳过第一行
。取(1); //选择第二行
I have looked at many sites and am not able to understand how I would go about using linq to query the first couple of rows of a data-table.
I would also like to know with regards to this if the data came from an Excel file would the column references be the same? For example column F in would be the same in the datatable or a numbered reference?
解决方案
DataTable yourDataTable = new DataTable();
var result = yourDataTable.AsEnumerable()
.Take(2) // Select first two rows
.Select(r =>
new
{
Field1 = r.Field<int>("col1"), // Select your columns
Field2 = r.Field<string>("col2")
// your rest of the columns
}
);
If you want to select only the second row then:
var result = yourDataTable.AsEnumerable()
.Skip(1) // skip first row
.Take(1) // Select second row
.Select(r =>
new
{
Field1 = r.Field<int>("col1"), // Select your columns
Field2 = r.Field<string>("col2")
// your rest of the columns
}
);
EDIT:
To Select all columns, instead of specific one, just remove the Select
from the statement. Something like this:
var result = yourDataTable.AsEnumerable()
.Skip(1) // skip first row
.Take(1); // Select second row
这篇关于数据表查询使用LINQ和C#的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文