0

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());
            }
        }
    }
}

3 Answers3

3

There is no need for the ArrayList. Just use the query in your 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
            };

And

foreach(var myProduct in stuff)

The problem is that you are trying to cast an anonymous type (created by the select new) to a tblmyWebsite in your foreach. When using anonymous types var is your friend.

juharr
  • 31,741
  • 4
  • 58
  • 93
3

You are populating your array list with anonymous types here:

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());

But then you try and do this:

foreach (tblmyWebsite myProduct in myArrList)

Your array list doesn't contain tblmyWebsite objects, it contains anonymous types. Even if they have the same fields with the same names, it won't convert them for you automatically. My assumption is that you tblmyWebsite is what you get from your database (in other words, db.tblmyWebsites is a collection of tblmyWebsite objects). So what you can do is simply:

myArrList.AddRange(db.tblmyWebsites);    // your array list is now populated with 
                                         // tblmyWebsite objects 

There is no need to use select unless you actually need to change or remap something. There's also no real point in using ArrayList, generic List<T> is much easier to use. You could, in fact, just do:

 foreach (var myProduct in db.tblmyWebsites)
Matt Burland
  • 44,552
  • 18
  • 99
  • 171
2

The ArrayList data structure is a non-generic list. It was introduced prior to the concept of generics, and it offers no benefits over the generic List<T>, except to support assemblies targetting the lower versions of the framework (compatability): https://stackoverflow.com/a/2309699/347172


That being said, you don't need to create a list from your enumeration if all you are simply using it in a foreach statement. Foreach works off of the IEnumerable/IEnumerable<T> interfaces, which is what your LINQ statement returns (an IEnumerable<T>).

foreach (var variable in LINQ-statement)
{
    // ...
}
Community
  • 1
  • 1
myermian
  • 31,823
  • 24
  • 123
  • 215
  • Thank you for the help And I think I need to spend some time to understand the technique behind . Because the example was using connection string and class of the database, but I try to do the same sample with linq – m_solaiman Oct 08 '14 at 15:51
  • I'll try to see the other solutions and verify it, and thank for all those who try to help – m_solaiman Oct 08 '14 at 15:53