LinqToSql查询到数据表无效的强制转换异常

时间:2019-06-07 12:03:18

标签: c# linq-to-sql

您好,这是创建DataTables的linqtosql查询:

private void GetStock()
    {
        using (DataClasses1DataContext dc = new DataClasses1DataContext())
        {

            #region ARTICLES
            DataTable dt = new DataTable("FICHES_ARTICLES");
            var article = from a in dc.FICHES_ARTICLES
                      where a.ART_SITE == 7
                      select new
                      {
                          a.ART_CODE,
                          a.ART_SITE,
                          a.ART_LIBELLE1,
                          a.ART_LIBELLE2,
                          a.ART_SIGNEQ,
                          a.ART_EAN13,
                          a.ART_DLUO,
                          a.ART_RESSUAGE,
                          a.EMB_CODE
                      };
            DataColumn dc1 = dt.Columns.Add("ART_CODE", typeof(string));
            DataColumn dc2 = dt.Columns.Add("ART_SITE", typeof(int));
            DataColumn dc3 = dt.Columns.Add("ART_LIBELLE1", typeof(string));
            DataColumn dc4 = dt.Columns.Add("ART_LIBELLE2", typeof(string));
            DataColumn dc5 = dt.Columns.Add("ART_SIGNEQ", typeof(string));
            DataColumn dc6 = dt.Columns.Add("ART_EAN13", typeof(string));
            DataColumn dc7 = dt.Columns.Add("ART_DLUO", typeof(int));
            DataColumn dc8 = dt.Columns.Add("ART_RESSUAGE", typeof(double));
            DataColumn dc9 = dt.Columns.Add("EMB_CODE", typeof(string));
            DataRow dw;
            foreach (var item in article)
            {
                dw = dt.NewRow();
                dw["ART_CODE"] = item.ART_CODE;
                dw["ART_SITE"] = item.ART_SITE;
                dw["ART_LIBELLE1"] = item.ART_LIBELLE1;
                dw["ART_LIBELLE2"] = item.ART_LIBELLE2;
                dw["ART_SIGNEQ"] = item.ART_SIGNEQ;
                dw["ART_EAN13"] = item.ART_EAN13;
                dw["ART_DLUO"] = item.ART_DLUO;
                dw["ART_RESSUAGE"] = item.ART_RESSUAGE;
                dw["EMB_CODE"] = item.EMB_CODE;
                dt.Rows.Add(dw);
            }
            #endregion

            #region ART_PARAM
            DataTable dt1 = new DataTable("ART_PARAM");
            var param = from p in dc.FICHES_ARTICLES
                        where p.ART_SITE == 7
                        orderby p.ART_LIBELLE1
                        select new
                        {
                            p.ART_CODE,
                            p.ART_SITE,
                            p.ART_LIBELLE1
                        };
            DataColumn dc10 = dt1.Columns.Add("ART_CODE", typeof(string));
            DataColumn dc11 = dt1.Columns.Add("ART_SITE", typeof(int));
            DataColumn dc12 = dt1.Columns.Add("ART_LIBELLE1", typeof(string));
            DataRow dw1;
            foreach (var c in param)
            {
                dw1 = dt1.NewRow();
                dw1["ART_CODE"] = c.ART_CODE;
                dw1["ART_SITE"] = c.ART_SITE;
                dw1["ART_LIBELLE1"] = c.ART_LIBELLE1;
                dt1.Rows.Add(dw1);
            }
            #endregion

            #region ENTREES_STOCKS
            DataTable dt2 = new DataTable("ENTREES_STOCKS");

            var ent = from e in dc.ENTREES_STOCKS
                      select new
                      {
                          e.ID,
                          e.ART_CODE,
                          e.ENTSTK_DTENTREE,
                          e.ENTSTK_PICKING,
                          e.ENTSTK_LOT,
                          e.ENTSTK_PNET,
                          e.ENTSTK_NBU,
                          e.ENTSTK_DATE_DEM,
                          e.ENTSTK_USER
                      };
            DataColumn dc13 = dt2.Columns.Add("ID", typeof(int));
            DataColumn dc14 = dt2.Columns.Add("ART_CODE", typeof(string));
            DataColumn dc15 = dt2.Columns.Add("ENTSTK_DTENTREE", typeof(DateTime));
            DataColumn dc16 = dt2.Columns.Add("ENTSTK_PICKING", typeof(string));
            DataColumn dc17 = dt2.Columns.Add("ENTSTK_LOT", typeof(int));
            DataColumn dc18 = dt2.Columns.Add("ENTSTK_PNET", typeof(float));
            DataColumn dc19 = dt2.Columns.Add("ENTSTK_NBU", typeof(int));
            DataColumn dc20 = dt2.Columns.Add("ENTSTK_DATE_DEM", typeof(DateTime));
            DataColumn dc21 = dt2.Columns.Add("ENTSTK_USER", typeof(string));
            DataRow dw2;
            foreach (var c in ent)
            {
                dw2 = dt2.NewRow();
                dw2["ID"] = c.ID;
                dw2["ART_CODE"] = c.ART_CODE;
                dw2["ENTSTK_DTENTREE"] = c.ENTSTK_DTENTREE;
                dw2["ENTSTK_PICKING"] = c.ENTSTK_PICKING;
                dw2["ENTSTK_LOT"] = c.ENTSTK_LOT;
                dw2["ENTSTK_PNET"] = c.ENTSTK_PNET;
                dw2["ENTSTK_NBU"] = c.ENTSTK_NBU;
                //dw2["ENTSTK_DATE_DEM"] = c.ENTSTK_DATE_DEM;
                if (dw2["ENTSTK_DATE_DEM"] != DBNull.Value)
                {
                    dw2["ENTSTK_DATE_DEM"] = c.ENTSTK_DATE_DEM;
                }
                dw2["ENTSTK_USER"] = c.ENTSTK_USER;
                dt2.Rows.Add(dw2);
            }
            #endregion

            #region DataSet
            DataSet ds = new DataSet();
            ds.Tables.Add(dt);
            ds.Tables.Add(dt1);
            ds.Tables.Add(dt2);
            #endregion

            #region Create Crystal Report
            string reportPath = DocNameTB.Text;
            ReportDocument cr = new ReportDocument();
            cr.Load(reportPath);
            cr.SetDataSource(ds);
            Cr_Viewer.ViewerCore.ReportSource = cr;
            #endregion

        }
    }

在第一个区域“ ARTICLES”中,我在该行遇到错误 “ foreach(文章中的可变项)”:强制转换异常 我不知道为什么 事实是,此代码对于其他所有区域都是相似的(并且可以正常工作)...

Error Message

谢谢您的帮助。

1 个答案:

答案 0 :(得分:1)

您可能要考虑使用扩展方法来自动执行此操作,这还将消除(数据)键入问题:

[http]
check-revoke = false