Click here to Skip to main content
14,869,660 members
Please Sign up or sign in to vote.
4.00/5 (1 vote)
See more:
I have the following class declaration:
C#
<blockquote class="FQ"><div class="FQA">Quote:</div> public class EventGroup
    {
        private readonly Guid id;
        private readonly string name;
        private readonly string description;
        private readonly Guid schoolId;     
       
        public EventGroup(Guid id, string name, string description, Guid schoolID) 
        {
            this.id = id;          
            this.schoolId = schoolID;
            this.name = name;
            this.description = description;
        }      
       public Guid SchoolID
        {
            get { return schoolId; }
        }
        public string Description
        {
            get { return description; }
        }
        public string Name
        {
            get { return name; }
        }
        public Guid ID
        {
            get { return id; }
        }       
       
    }


When I declare the following its working
C#
<blockquote class="FQ"><div class="FQA">Quote:</div>

List<EventGroup> lstGroup = new List<EventGroup>();
var eventt_grp= lstGroup.Select(r => new {r.ID,r.Name});


But its not working if I do like this and getting the error:
Quote:
Cannot implicitly convert type 'System.Collections.Generic.IEnumerable<anonymoustype#1>' to 'System.Collections.Generic.List<tools.businessobjects.eventgroup>'. An
explicit conversion exists (are you missing a cast?)


C#
<blockquote class="FQ"><div class="FQA">Quote:</div>

List<EventGroup> eventt_grp1 = lstGroup.Select(r => new {r.ID, r.Name });
Posted

Use var and don't create a list at all. You need to call ToList at the end of your Select in order to get back a list, instead of an IEnumerable.

List<eventgroup> eventt_grp1 = lstGroup.Select(r => new {r.ID, r.Name }).ToList();

If you don't want to use var, you need to define a class that has ID and Name properties. You need to use var because you're using an anonymous class.
   
v2
Comments
Rahul Rajat Singh 13-Dec-12 4:43am
   
IMHO, this will also not work because the result has to be in a the generic list that is expecting some type and the projected type is anonymous and thus we will never be able to specify the type of list of the result type variable.
Tirthankar Dutta 13-Dec-12 4:50am
   
May you kindly help how this can be written without using var?
Rahul Rajat Singh 13-Dec-12 5:01am
   
If you don't want to use var then you will have to utilize the fact that utlimately whatever is being returned is enumerable. so try this and this should work.

IEnumerable eventt_grp = lstGroup.Select(r => new {r.ID, r.Name });
Christian Graus 13-Dec-12 4:44am
   
I think you are right. So he needs to just use var.
Rahul Rajat Singh 13-Dec-12 5:01am
   
or he can try this:

IEnumerable eventt_grp = lstGroup.Select(r => new {r.ID, r.Name });
Balaji GJ 19-Feb-13 1:52am
   
xsxc
Member 12850869 28-Sep-17 5:54am
   
f
Luan Cardoso 9-Aug-19 7:36am
   
Thank you so much, it worked fine to me (.ToList();)
Love you <3
This is because you are returning an anonymous type from your Select and you are trying to store it in the List<eventgroup>. The projections always create anonymous types.

Also, if you only need to have ID and name in the results why are you storing them in a list of List<eventgroup>. using var should be fine. If you must have the result in the List<eventgroup> then there is something wrong and you might want to reconsider the logic/design.

If you don't want to use var then you will have to utilize the fact that utlimately whatever is being returned is enumerable. so try this and this should work.
C#
IEnumerable eventt_grp = lstGroup.Select(r => new {r.ID, r.Name });
   
v3

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)




CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900