无法投射'<>类型的对象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