【发布时间】:2017-05-18 06:28:49
【问题描述】:
您好先生,我不是英语母语,stackoverflow 中的新手和编程方面的新手,但我会尽力与您分享我的问题: 我在代码中添加了一些 cmets,希望您能更好地了解问题所在 我正在尝试制作一个临时数据表之类的东西,它从 1 个特定数据表中获取信息(只有行很重要)(代码中会有更多内容),并且“临时数据表”将这些信息提供给列表我用 linq 尝试过. ofc 我有自己的想法,并试图以我理解的方式改变它(LINQ query on a DataTable 这对我没有真正帮助:X),我也尝试了其他一些事情,但我不想在这里粉碎 10 个链接:P 所以代码来了:
public MainWindow()
{
InitializeComponent();
datatable1();
}
public void datatable1()
{
/*This Table should get the informations from datatable_1 or
another one (there will be some more tables and the viewtable will
get the informations from the table where the
Type ==(i guess it will be a combobox) selected Type */
DataTable viewtable = new DataTable();
viewtable.Columns.Add("Typ", typeof(string));
viewtable.Columns.Add("Name", typeof(string));
viewtable.Columns.Add("Anzahl", typeof(string));
viewtable.Columns.Add("Zeit", typeof(string));
/*here is the main problem i have*/
viewtable.Rows =from _Row1 in datatable_1 where "Typ" =="Una";
/*it "worked" like this so i get the informations in my list*/
viewtable.Rows.Add("Una", "Testschrank2", "9000", "0:20:30");
//this table is a example table holding the informations
DataTable datatable_1 = new DataTable();
datatable_1.Clear();
datatable_1.Columns.Add("Typ");
datatable_1.Columns.Add("Name");
datatable_1.Columns.Add("Anzahl");
datatable_1.Columns.Add("Zeit");
DataRow _Row1 = datatable_1.NewRow();
datatable_1.Rows.Add("Una", "Testschrank2", "9000", "0:20:30");
// _Row1["Zeit"] = (4, 30, 0);
datatable_1.Rows.Add(_Row1);`
}
好吧,我想我添加了太多代码,但就像我说的那样,我对此真的很陌生,所以我很难用很少的代码指出我的问题,对不起先生 和 谢谢你的帮助o/
【问题讨论】:
-
你到底想做什么?
-
关键部分:
var fromRow = fromTable.Rows.OfType<DataRow>().First(x=>someconvert x[columnIndex]==yourvalue); toTable.ImportRow(fromRow); -
这是您的代码在程序中的顺序吗?看起来它不会这样编译
-
我希望像这样清楚我正在尝试从数据表视图中的 datatable_1 获取行,但这样它不起作用
-
一切都很好,除了以下行:viewtable.Rows =from _Row1 in datatable_1 where "Typ" =="Una" ;