【问题标题】:Cannot implicitly convert type System.Data.EnumerableRowCollection<Prog.tblName> to Prog.tblName无法将类型 System.Data.EnumerableRowCollection<Prog.tblName> 隐式转换为 Prog.tblName
【发布时间】:2013-11-07 02:39:49
【问题描述】:

我正在尝试使用 LINQ to SQL 将一些记录从 Access 导入 SQL 服务器,并且在以下代码中的“选择新 tblName”(选择带有下划线)处收到了主题中的错误:

        OdbcConnection myconnection = new OdbcConnection("Driver={Microsoft Access Driver (*.mdb, *.accdb)};Dbq=" + this.txtFullPath.Text);
        OdbcDataAdapter myadapter = new OdbcDataAdapter("SELECT * FROM Name", myconnection);
        DataSet myCustomersDS = new DataSet();
        myadapter.Fill(myCustomersDS, "Name");

        //LINQ
        iMISDataContext db = new iMISDataContext();

        // inserting multiple items
        tblName toInsert =
            from row in myCustomersDS.Tables["Name"].AsEnumerable()
            select new tblName
            {
                ID = row.Field<string>("ID"),
                ORG_CODE = row.Field<string>("ORG_CODE"),
                MEMBER_TYPE = row.Field<string>("MEMBER_TYPE"),
                //... and rest of the columns (lots of columns)
            };
        db.tblNames.InsertAllOnSubmit(toInsert);
        db.SubmitChanges();

最后第二行还有另一个错误: 无法从用法中推断方法“System.Data.Linq.Table.InsertAllOnSubmit(System.Collections.Generic.IEnumerable)”的类型参数。尝试明确指定类型参数。

谢谢你, 史蒂文

【问题讨论】:

  • InsertAllOnSubmit的签名是什么?

标签: c# linq


【解决方案1】:

当您查询对myCustomersDS.Tables["Name"] 中的行进行投影时,您实际上是在选择一些对象而不是单个对象。所以改变

// inserting multiple items
        tblName toInsert =
            from row in myCustomersDS.Tables["Name"].AsEnumerable()
            .......

到:

// inserting multiple items
        var toInsert =
            from row in myCustomersDS.Tables["Name"].AsEnumerable()
            .....

【讨论】:

  • 谢谢。我之前有var,它为我加了下划线,我认为它是一个变量声明,但我知道Pascal或Delphi是使用“var”来声明变量,所以我替换为类型。 var 在这里是什么意思?谢谢,史蒂文
  • var 是变量类型的快捷方式。这里可以替换为 EnumerableRowCollection
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-01-14
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-08-18
  • 2022-01-15
相关资源
最近更新 更多