Cannot implement type XYZ with a collection initializer because it does not implement 'System.Collections.IEnumerable'

25,223

Solution 1

Make your fields public because they are not accessible as they are now.

public class CommentList
{
  public string ItemType;
  public string Comment1;
  public string Status1;
  public string DiscussionBoardId;
  public Guid CourseId;
  public Guid CommentID;
}

And explicity set them in your initializer.

select new CommentList
{
    ItemType = d.ItemType,
    Comment1 = c.Comment1,
    Status1 = s.Status1,
    DiscussionBoardId = c.DiscussionBoardId,
    CourseId = d.CourseId,
    CommentID = c.CommentID
};

Solution 2

Just a fuller follow-up to my comment. What you are trying to do is use object initialization, which requires you to name the properties. What it thinks you are trying is collection initialization

List<CommentList> query = from c in db.Comments
                join s in db.Status on c.StatusId equals s.StatusId
                join d in db.DiscussionBoards on c.DiscussionBoardId equals d.DiscussionBoardId
                where d.CourseId == "CourseID"
                orderby d.ItemType, d.DiscussionBoardId
                select new CommentList
                {
                    ItemType = d.ItemType,
                    Comment1 = c.Comment1,
                    Status1= s.Status1,
                    DiscussionBoardId = c.DiscussionBoardId,
                    CourseId = d.CourseId,
                    CommentId = c.CommentID
                };

Solution 3

Your initialization is not correct. You need to assign values to explicit properties.

select new CommentList
{
    ItemType = d.ItemType,
    Comment1 = c.Comment1,
    Status1 = s.Status1,
    DiscussionBoardId = c.DiscussionBoardId,
    CourseId = d.CourseId,
    CommentID = c.CommentID
};

Solution 4

You can only infer property names when creating anonymous types.

You need to explicitly assign your properties:

new CommentList {
    ItemType = d.ItemType, 
    ...
Share:
25,223
Kevin
Author by

Kevin

For years I developed ILE RPGIV software for the IBM AS/400 iSeries midrange computer. I made the leap to .Net years ago and love it. I've worked in a variety of industries from manufacturing, financial, and defense. I'm constantly amazed that even to this day, I learn something new every single day.

Updated on August 29, 2020

Comments

  • Kevin
    Kevin over 3 years

    I have the following class:

    public class CommentList 
    {
        string ItemType;
        string Comment1;
        string Status1;
        string DiscussionBoardId;
        Guid CourseId;
        Guid CommentID;
    }
    

    I'm trying to do the following LINQ statement:

    List<CommentList> query=
        from c in db.Comments
        join s in db.Status on c.StatusId equals s.StatusId
        join d in db.DiscussionBoards 
            on c.DiscussionBoardId equals d.DiscussionBoardId
        where d.CourseId=="CourseID"
        orderby d.ItemType, d.DiscussionBoardId
        select new CommentList {
            d.ItemType,
            c.Comment1,
            s.Status1,
            c.DiscussionBoardId,
            d.CourseId,
            c.CommentID
        };
    

    The problem is, the editor is complaining on the first parenthesis of the select statement. It's saying:

    Cannot implement type 'CommentList' with a collection initializer because it does not implement 'System.Collections.IEnumerable'.

    Can someone help me out and tell me what I'm doing wrong?