2014-10-08 82 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