Unable to cast object of type '<>f__AnonymousType0`7[System.Int32,System.String,System.String,System.String,System.String,System.String,System.Int32]' to type 'myWebApplication.tblmyWebsite
I'm very newbie to c# Could anyone here tell me what's the problem in this code please?
AnonymousType0 when I need to get records from LINQ to SQL and show it to the browser using ArrayList.
see code please
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());
}
}
}
}