我已经看到其他解决方案非常类似我的问题,但不知何故它不适用于我。如何获取数据表和数据库的强类型linq查询
我有一个LINQ查询到数据库的连接到非强类型的数据表。 我需要一个强类型的结果来返回一个视图。
我尝试看起来像这样
var query = (from t in queryAllTasks
join tL in table.AsEnumerable() on t.TaskId equals tL.Field<int>("TaskId")
select new { t.TaskId, Kasten = tL.Field<int>("Box") }).AsEnumerable();
下面是数据表,如果其感兴趣的:
//Create new DataTable.
DataTable table = new DataTable();
//Declare DataColumn and DataRow variables
DataColumn column;
DataRow row;
//Create ne DataColumn
//set DataType
//ColumnName
//add to Datatable
column = new DataColumn();
column.DataType = System.Type.GetType("System.Int32");
column.ColumnName = "TaskId";
table.Columns.Add(column);
//Dritte Spalte
column = new DataColumn();
column.DataType = System.Type.GetType("System.Int32");
column.ColumnName = "Box";
table.Columns.Add(column);
您选择的匿名类型是强类型的,那么问题是什么? –
如果没有明显的错误,那么问题可能就是asp.net问题,而不是我的c#代码。因为它抱怨不能永存。 – Wozilla
对于什么?您的查询已经是'IEnumerable <匿名类型>',即使没有多余的AsEnumerable也是如此。也许你需要一些不是匿名类型的东西。 –