【发布时间】: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?