C# 数据表上的 Linq:选择特定列到数据表中,而不是整个表
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/14422655/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Linq on DataTable: select specific column into datatable, not whole table
提问by Uri Klar
I'm running a LINQ query on a datatable
in c#.
我正在datatable
c# 中的 a上运行 LINQ 查询。
I want to select specific columns rather than the whole row and enter the result into a datatable
. How can i do that??
我想选择特定的列而不是整行并将结果输入到datatable
. 我怎样才能做到这一点??
My Code:
我的代码:
public DataTable getConversions(string c_to, string p_to)
{
var query = from r in matrix.AsEnumerable()
where r.Field<string>("c_to") == c_to &&
r.Field<string>("p_to") == p_to
select r;
DataTable conversions = query.CopyToDataTable();
采纳答案by OJ Raque?o
If you already know beforehand how many columns your new DataTable would have, you can do something like this:
如果您事先知道新 DataTable 将有多少列,您可以执行以下操作:
DataTable matrix = ... // get matrix values from db
DataTable newDataTable = new DataTable();
newDataTable.Columns.Add("c_to", typeof(string));
newDataTable.Columns.Add("p_to", typeof(string));
var query = from r in matrix.AsEnumerable()
where r.Field<string>("c_to") == "foo" &&
r.Field<string>("p_to") == "bar"
let objectArray = new object[]
{
r.Field<string>("c_to"), r.Field<string>("p_to")
}
select objectArray;
foreach (var array in query)
{
newDataTable.Rows.Add(array);
}
回答by Adil
Your select statement is returning a sequence of anonymous type , not a sequence of DataRows. CopyToDataTable() is only available on IEnumerable<T>
where T
is or derives from DataRow
. You can select r
the row object to call CopyToDataTable on it.
您的 select 语句返回的是一个匿名类型序列,而不是 DataRows 序列。CopyToDataTable() 仅适用于IEnumerable<T>
where T
is 或源自DataRow
。您可以选择r
行对象以对其调用 CopyToDataTable。
var query = from r in matrix.AsEnumerable()
where r.Field<string>("c_to") == c_to &&
r.Field<string>("p_to") == p_to
select r;
DataTable conversions = query.CopyToDataTable();
You can also implement CopyToDataTableWhere the Generic Type T Is Not a DataRow.
您还可以在通用类型 T 不是 DataRow 的情况下实现 CopyToDataTable。
回答by Leax
Here I get only three specific columns from mainDataTable and use the filter
在这里我只从 mainDataTable 得到三个特定的列并使用过滤器
DataTable checkedParams = mainDataTable.Select("checked = true").CopyToDataTable()
.DefaultView.ToTable(false, "lagerID", "reservePeriod", "discount");
回答by Sagar Upadhyay
Try Access DataTable easiest waywhich can help you for getting perfect idea for accessing DataTable, DataSet using Linq...
尝试访问 DataTable 最简单的方法,它可以帮助您获得使用 Linq 访问 DataTable、DataSet 的完美想法...
Consider following example, suppose we have DataTablelike below.
考虑以下示例,假设我们有如下所示的DataTable。
DataTable ObjDt = new DataTable("List");
ObjDt.Columns.Add("WorkName", typeof(string));
ObjDt.Columns.Add("Price", typeof(decimal));
ObjDt.Columns.Add("Area", typeof(string));
ObjDt.Columns.Add("Quantity",typeof(int));
ObjDt.Columns.Add("Breath",typeof(decimal));
ObjDt.Columns.Add("Length",typeof(decimal));
Here above is the code for DatTable, here we assume that there are some data are available in this DataTable, and we have to bind Grid view of particular by processing some data as shown below.
上面是DatTable的代码,这里我们假设这个DataTable中有一些数据可用,我们必须通过处理一些数据来绑定特定的Grid视图,如下所示。
Area| Quantity| Breath| Length| Price = Quantity * breath *Length
地区| 数量| 呼吸| 长度| 价格=数量*呼吸*长度
Than we have to fire following query which will give us exact result as we want.
然后我们必须触发以下查询,这将为我们提供我们想要的确切结果。
var data = ObjDt.AsEnumerable().Select
(r => new
{
Area = r.Field<string>("Area"),
Que = r.Field<int>("Quantity"),
Breath = r.Field<decimal>("Breath"),
Length = r.Field<decimal>("Length"),
totLen = r.Field<int>("Quantity") * (r.Field<decimal>("Breath") * r.Field<decimal>("Length"))
}).ToList();
We just have to assign this data variable as Data Source.
我们只需要将此数据变量分配为数据源。
By using this simple Linq query we can get all our accepts, and also we can perform all other LINQ queries with this…
通过使用这个简单的 Linq 查询,我们可以获得所有的接受,并且我们还可以使用这个来执行所有其他的 LINQ 查询……
回答by Tapan kumar
LINQ is very effective and easy to use on Lists rather than DataTable. I can see the above answers have a loop(for, foreach), which I will not prefer.
LINQ 在 Lists 而非 DataTable 上非常有效且易于使用。我可以看到上面的答案有一个循环(for,foreach),我不喜欢。
So the best thing to select a perticular column from a DataTable is just use a DataView to filter the column and use it as you want.
因此,从 DataTable 中选择特定列的最佳方法是使用 DataView 过滤该列并根据需要使用它。
Find it here how to do this.
在此处找到如何执行此操作。
DataView dtView = new DataView(dtYourDataTable);
DataTable dtTableWithOneColumn= dtView .ToTable(true, "ColumnA");
Now the DataTable dtTableWithOneColumncontains only one column(ColumnA).
现在 DataTable dtTableWithOneColumn只包含一列(ColumnA)。