【问题标题】:assign data from LINQ to ArrayList将数据从 LINQ 分配给 ArrayList
【发布时间】:2014-10-08 21:29:34
【问题描述】:

无法将 'f__AnonymousType0`7[System.Int32,System.String,System.String,System.String,System.String,System.String,System.Int32]' 类型的对象转换为类型 'myWebApplication. tblmy网站

我是 C# 的新手 谁能告诉我这段代码有什么问题?

AnonymousType0 当我需要从 LINQ to SQL 获取记录并使用 ArrayList 将其显示给浏览器时。

请看代码

    using System;
    using System.Collections; // so you can use ArrayList.
    using System.Text; // so you can use StringBuilder.
    using System.Collections.Generic;
    using System.Linq;
    using System.Web;
    using System.Web.UI;
    using System.Web.UI.WebControls;

namespace myWebApplication
{
    public partial class WebForm2 : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            showProducts();
        }
        private void showProducts()
        {
            dbDataContext db = new dbDataContext();

            var products = from p in db.tblmyWebsites
                           select p ;
            GridView1.DataSource = products;
            GridView1.DataBind();

            // the way to convert LINQ query to ArrayList
            ArrayList myArrList = new ArrayList();
            myArrList.AddRange((from p in db.tblmyWebsites
                                select new
                                {
                                    p.Id,
                                    p.productName,
                                    p.tblprogrammingLanguage.programmingLanguage, 
                                    p.tblapplicationType.applicationType,
                                    p.image,
                                    p.review, 
                                    p.price}).ToList());

            // StringBuilder Represents a mutable string of characters.
            StringBuilder sb = new StringBuilder();

            foreach (tblmyWebsite myProduct in myArrList)
            {
                sb.Append(string.Format(@"<table class = 'coffeeTable'>


                       <tr>
                           <th>Id: </th>
                           <td>{1}</td>
                       </tr>

                       <tr>
                           <th>productName: </th>
                           <td>{2}</td>
                       </tr>

                       <tr>
                           <th>programmingLanguage: </th>
                           <td>{3}</td>
                       </tr>

                       <tr>
                           <th>type: </th>
                           <td>{4}</td>
                       </tr>
                        <tr>
                           <th>image: </th>
                           <td>{5}</td>
                       </tr>

                       <tr>
                           <th>review: </th>
                           <td>{6}</td>
                       </tr>

                       <tr>
                           <th>price: </th>
                           <td>{7}</td>
                       </tr>
                       </table> ", myProduct.Id, myProduct.productName, myProduct.programmingLanguage, myProduct.type,
                                 myProduct.image, myProduct.review, myProduct.price).ToString());
            }
        }
    }
}

【问题讨论】:

  • 是否有任何理由将 LINQ 枚举转换为非泛型 ArrayList

标签: c# linq


【解决方案1】:

您在此处使用匿名类型填充数组列表:

myArrList.AddRange((from p in db.tblmyWebsites
                            select new
                            {
                                p.Id,
                                p.productName,
                                p.tblprogrammingLanguage.programmingLanguage, 
                                p.tblapplicationType.applicationType,
                                p.image,
                                p.review, 
                                p.price}).ToList());

然后你尝试这样做:

foreach (tblmyWebsite myProduct in myArrList)

您的数组列表不包含tblmyWebsite 对象,它包含匿名类型。即使它们具有相同名称的相同字段,它也不会自动为您转换它们。我的假设是您tblmyWebsite 是您从数据库中获得的(换句话说,db.tblmyWebsites 是tblmyWebsite 对象的集合)。所以你可以做的很简单:

myArrList.AddRange(db.tblmyWebsites);    // your array list is now populated with 
                                         // tblmyWebsite objects 

除非您确实需要更改或重新映射某些内容,否则无需使用select。使用ArrayList 也没有实际意义,通用List&lt;T&gt; 更容易使用。事实上,你可以这样做:

 foreach (var myProduct in db.tblmyWebsites)

【讨论】:

    【解决方案2】:

    不需要ArrayList。只需使用foreach 中的查询即可。

    var stuff = from p in db.tblmyWebsites
                select new
                {
                    p.Id,
                    p.productName,
                    p.tblprogrammingLanguage.programmingLanguage, 
                    p.tblapplicationType.applicationType,
                    p.image,
                    p.review, 
                    p.price
                };
    

    foreach(var myProduct in stuff)
    

    问题是您试图将匿名类型(由select new 创建)转换为tblmyWebsite 中的foreach。当使用匿名类型时,var 是你的朋友。

    【讨论】:

      【解决方案3】:

      ArrayList 数据结构是一个非泛型列表。它是在泛型概念之前引入的,与泛型 List&lt;T&gt; 相比没有任何好处,除了支持针对较低版本框架的程序集(兼容性):https://stackoverflow.com/a/2309699/347172


      话虽如此,如果您只是在 foreach 语句中使用它,则无需从枚举中创建列表。 Foreach 使用 IEnumerable/IEnumerable&lt;T&gt; 接口工作,这是您的 LINQ 语句返回的内容(IEnumerable&lt;T&gt;)。

      foreach (var variable in LINQ-statement)
      {
          // ...
      }
      

      【讨论】:

      • 感谢您的帮助我想我需要花一些时间来了解背后的技术。因为该示例使用数据库的连接字符串和类,但我尝试使用 linq 进行相同的示例
      • 我会尝试查看其他解决方案并进行验证,并感谢所有尝试提供帮助的人
      猜你喜欢
      • 2016-07-01
      • 1970-01-01
      • 2016-01-08
      • 1970-01-01
      • 1970-01-01
      • 2011-04-14
      • 2015-11-17
      • 2013-02-05
      • 1970-01-01
      相关资源
      最近更新 更多