無法投射'<>類型的對象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());
}
}
}
}
是否有任何理由將LINQ枚舉變成非泛型'ArrayList'? – 2014-10-08 13:21:44