2014-10-08 91 views
0

无法投射'<>类型的对象f__AnonymousType0`7 [System.Int32,System.String,System.String,System.String,System.String,System.String,System.Int32] '键入'myWebApplication.tblmyWebsite将数据从LINQ分配给ArrayList

我很新手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()); 
      } 
     } 
    } 
} 
+2

是否有任何理由将LINQ枚举变成非泛型'ArrayList'? – 2014-10-08 13:21:44

回答

3

您在这里填充有匿名类型的数组列表:

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<T>使用起来更容易。事实上,你可以这样做:

foreach (var myProduct in db.tblmyWebsites) 
3

没有必要为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) 

的问题是,你想在你的foreach投匿名类型(由select new创建)tblmyWebsite。当使用匿名类型var是你的朋友。

2

ArrayList数据结构是一个非泛型列表。这之前,仿制药的概念被引入,并在通用List<T>提供任何好处,除了支撑组件靶向较低版本的框架(兼容性):https://stackoverflow.com/a/2309699/347172


话虽这么说,你不”如果您只是简单地在foreach声明中使用它,则需要从枚举中创建列表。 Foreach在IEnumerable/IEnumerable<T>接口中工作,这是LINQ语句返回的结果(一个IEnumerable<T>)。

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

谢谢你的帮助 我想我需要花一些时间来理解背后的技术。 由于该示例使用连接字符串和数据库类,但我尝试使用linq – 2014-10-08 15:51:15

+0

做同样的示例,我会尝试查看其他解决方案并验证它,并感谢所有尝试提供帮助的人员 – 2014-10-08 15:53:04