连接两个数据表并将结果保存在数据表中

时间:2019-03-25 08:00:56

标签: c# asp.net

我有2个数据集,我想加入两个数据集并将结果保存在数据表中。

我可以联接表,但是将结果保存到数据表中时会抛出错误 “无法转换类型为d__38 4 [System.Data.DataRow,System.Data.DataRow,System.String,<> f__AnonymousType0 7 [System.String,System.String,System.Int32,System.Int32, System.String,System.String,System.String]]”键入“ System.Data.DataTable”

我的代码:

private DataSet joindatasets(DataSet ds1, DataSet dsHeader)
        {
            DataTable dt; 
            DataSet ds = new DataSet(); 
            DataTable dt1=new DataTable();

        dt1.Columns.Add("PracticeCode", typeof(string));
        dt1.Columns.Add("PracticeName", typeof(string));
        dt1.Columns.Add("High", typeof(int));
        dt1.Columns.Add("Medium", typeof(int));
        dt1.Columns.Add("username", typeof(string));
        dt1.Columns.Add("MIAlertHeader", typeof(string));
        dt1.Columns.Add("MIAlertFooter", typeof(string));

        if (dsHeader.Tables.Count > 0)
        {
            for (int i = 0; i <= dsHeader.Tables.Count - 1; i++)
            {
                dt = dsHeader.Tables[i];

                var query = from table1 in ds1.Tables[0].AsEnumerable()
                             join table2 in dt.AsEnumerable()
                             on (string)table1["PracticeCode"] equals (string)table2["PracticeCode"]


                            select new
                            {
                                PracticeCode = (string)table1["PracticeCode"],
                                PracticeName = (string)table1["PracticeName"],
                                High = (int)table1["High"],
                                Medium = (int)table1["Medium"],
                                username = (string)table2["username"],
                                MIAlertHeader = (string)table2["MIAlertHeader"],
                                MIAlertFooter = (string)table2["MIAlertFooter"]
                            };
                dt1 = (DataTable)query;

            }
        }

        return ds;
    }

请帮助

2 个答案:

答案 0 :(得分:1)

当您尝试将IEnumerable<object>转换为DataRow时,经常会发生这种错误。要解决此问题,您必须使用DataTable.LoadDataRow method + CopyToDataTable method

            dt1 = (from table1 in ds1.Tables[0].AsEnumerable()
                         join table2 in dt.AsEnumerable()
                         on (string)table1["PracticeCode"] equals (string)table2["PracticeCode"]
                        select dt1.LoadDataRow(new object[]
                        {
                            (string)table1["PracticeCode"],
                            (string)table1["PracticeName"],
                            (int)table1["High"],
                            (int)table1["Medium"],
                            (string)table2["username"],
                            (string)table2["MIAlertHeader"],
                            (string)table2["MIAlertFooter"]
                        }, false)).CopyToDataTable();

更多详细信息,您将在这里找到:Creating a DataTable From a Query (LINQ to DataSet)

答案 1 :(得分:0)

创建以下方法并传递LINQ查询返回的任何类型的对象。

public static DataTable CreateDataTableFromAnyCollection<T>(IEnumerable<T> list)
    {
        Type type = typeof(T);
        var properties = type.GetProperties();

        DataTable dataTable = new DataTable();
        foreach (PropertyInfo info in properties)
        {
            dataTable.Columns.Add(new DataColumn(info.Name, Nullable.GetUnderlyingType(info.PropertyType) ?? info.PropertyType));
        }

        foreach (T entity in list)
        {
            object[] values = new object[properties.Length];
            for (int i = 0; i < properties.Length; i++)
            {
                values[i] = properties[i].GetValue(entity,null);
            }

            dataTable.Rows.Add(values);
        }

        return dataTable;
    }

并像这样传递您的LINQ结果

DataTable dt = CreateDataTableFromAnyCollection(list);