tags:

views:

47

answers:

1

hello all,

This particular query is giving me an error please tell where am I going wrong

public IList <BC_FeedbackBy> GetFeedbackList()
        {
            int feedbackId = 0;
            string feedbackName = string.Empty;

            using (brandconnectionsEntities modelObject = new brandconnectionsEntities())
            {
                return (IList<BC_FeedbackBy>)(from s in modelObject.BC_FeedbackBy
                               select new 
                               {

                                  feedbackId =s.FeedbackById ,
                                  feedbackName=s.FeedbackBy  ,
                               })
                  .ToList ();
            }
        }

error is

Unable to cast object of type 'System.Collections.Generic.List
`1[<>f__AnonymousType0`2[System .Int32,System.String]]' to type 
'System.Collections.Generic.IList`1[BrandConnectionsPrototype.Models.BC_FeedbackBy]'.
+2  A: 

select new creates instances of a new anonymous type. The query creates a list of these and then you try to cast that to IList<BC_FeedbackBy>. Create new instances of BC_FeedbackBy in the query instead.

Something like

select new BC_FeedbackBy() 
{
   feedbackId =s.FeedbackById ,
   feedbackName=s.FeedbackBy  ,
})
Brian Rasmussen
You don't need a constructor for this, the default generated empty one works since you're using object initializers: http://msdn.microsoft.com/en-us/library/bb384062.aspx
Nick Craver
Doh, you're right. I was thinking about using the constructor, but as I just copied the code from the question I ended up using properties so the default constructor is enough for this. Thanks!
Brian Rasmussen