2017-11-25 217 views
0

linq查询返回的结果形式为{DataRow Job1, DataRow Run}。我怎样才能将JoinResult转换成数据表。将LINQ转换为DataTable

var JoinResult = (from Job1 in Jobdata.AsEnumerable() 
        join Product in Productdata.AsEnumerable() 
        on Job1.Field<int>("Job_Id") equals Product.Field<int>("Job_Id") 
        join Run in data.AsEnumerable() 
        on Job1.Field<int>("Run_Id") equals Run.Field<int>("Run_Id") 
        select new { Job1, Run }); 
+0

不清楚(例如,“Jobdata”和“Product”有数据表关系,为什么不使用常规数据表解决方案?)和广泛的。您是否在寻找现有解决方案?这不完全是一个新问题。 –

+0

所以你想要一个'DataTable'与每个数据行的列并排? –

回答

0

您可以通过循环槽查询结果中创建一个新的datatable

private DataTable createDt() 
{ 
    var JoinResult = (from Job1 in Jobdata.AsEnumerable() 
         join Product in Productdata.AsEnumerable() 
         on Job1.Field<int>("Job_Id") equals Product.Field<int>("Job_Id") 
         join Run in data.AsEnumerable() 
         on Job1.Field<int>("Run_Id") equals Run.Field<int>("Run_Id") 
         select new { Job1, Run }); 

    DataTable newdt = new DataTable(); 

    // declare strongly typed data columns 
    DataColumn run = new DataColumn("run"); 
    run.DataType = System.Type.GetType("System.Int32"); 
    newdt.Columns.Add(run); 

    DataColumn job1 = new DataColumn("job1"); 
    job1.DataType = System.Type.GetType("System.Int32"); 
    newdt.Columns.Add(job1); 


    foreach (var x in JoinResult) 
    { 
     DataRow dr = newdt.NewRow(); 
     dr["run"] = x.Run; 
     dr["job1"] = x.Job1; 
     newdt.Rows.Add(dr); 
    } 

    return newdt; 
} 

顺便说一句 - 你不能在你的情况下使用CopyToDataTable()Why am I not getting .CopyToDataTable() in Linq Query()

,但如果你坚持使用它: How to: Implement CopyToDataTable Where the Generic Type T Is Not a DataRow

+0

,但按照它应该工作:https://docs.microsoft.com/en-us/dotnet/framework/data/adonet/creating-a-datatable-from-a-query-linq-to-dataset –

0

您可以创建一个扩展方法&使用它如下:

/// <Summary> 
    /// Convert a IEnumerable to a DataTable. 
    /// <TypeParam name="T">Type representing the type to convert.</TypeParam> 
    /// <param name="source">List of requested type representing the values to convert.</param> 
    /// <returns> returns a DataTable</returns> 
    /// </Summary> 
    public static DataTable ToDataTable<T>(this IEnumerable<T> source) 
    { 
     // Use reflection to get the properties for the type we’re converting to a DataTable. 
     var props = typeof(T).GetProperties(); 

     // Build the structure of the DataTable by converting the PropertyInfo[] into DataColumn[] using property list 
     // Add each DataColumn to the DataTable at one time with the AddRange method. 
     var dt = new DataTable(); 
     dt.Columns.AddRange(
      props.Select(p => new DataColumn(p.Name, BaseType(p.PropertyType))).ToArray()); 

     // Populate the property values to the DataTable 
     source.ToList().ForEach(
      i => dt.Rows.Add(props.Select(p => p.GetValue(i, null)).ToArray()) 
     ); 

     return dt; 
    } 


//To call the above method: 
var dt = JoinResult.ToDataTable(); 

注意:您只需要更新您的linq查询以获取IEnumerable数据。 希望它可以帮助你。