tags:

views:

88

answers:

2

Hello All,

I want this below class to return IList<> Please tell me why it is not working

public IList<CheckBoxListInfo> GetLinks()
        {
            string linkName = string.Empty;
            int linkId = 0;
            using (var db = new brandconnectionsEntities())
            {
                var query = from s in db.BC_TabTable
                                                 select new
                                                 {

                                                     linkName = s.TabName,
                                                     linkId = s.TabId,
                                                 };

                IList<CheckBoxListInfo> list = query.ToList() as IList<CheckBoxListInfo>;

                return list;

            }
        }

Thanks Ritz

+5  A: 

You need to change your select statement to return CheckBoxListInfo objects.

e.g.

select new CheckBoxListInfo 
{
    LinkName = s.TabName, 
    LinkId = s.TabId, 
}; 

At the moment it is returning an anonymous type.

AdamRalph
A: 

It doesn't work because right now your query returns a sequence of an anonymous type defined by this expression:

select new 
{ 
    linkName = s.TabName, 
    linkId = s.TabId, 
};

Instead, you need to select a new CheckBoxListInfo in your query, like this pseudo-code:

select new CheckBoxListInfo { LinkName = s.TabName, LinkId = s.TabId }

(I don't know how the CheckBoxListInfo API looks, so YMMV.

Mark Seemann