Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: C# ASP.NET ASP:NET
I have the following class declaration:
<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
<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'. An
explicit conversion exists (are you missing a cast?)

 
<blockquote class="FQ"><div class="FQA">Quote:</div>
 
List<EventGroup> eventt_grp1 = lstGroup.Select(r => new {r.ID, r.Name });
Posted 12-Dec-12 23:23pm
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 1

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 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.
  Permalink  
v2
Comments
Rahul Rajat Singh at 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 at 13-Dec-12 4:50am
   
May you kindly help how this can be written without using var?
Rahul Rajat Singh at 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 at 13-Dec-12 4:44am
   
I think you are right. So he needs to just use var.
Rahul Rajat Singh at 13-Dec-12 5:01am
   
or he can try this:
 
IEnumerable eventt_grp = lstGroup.Select(r => new {r.ID, r.Name });
gjbala at 19-Feb-13 1:52am
   
xsxc
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 2

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.
IEnumerable eventt_grp = lstGroup.Select(r => new {r.ID, r.Name });
  Permalink  
v3

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

  Print Answers RSS
0 OriginalGriff 304
1 Sergey Alexandrovich Kryukov 295
2 Shweta N Mishra 216
3 Maciej Los 210
4 PIEBALDconsult 184
0 OriginalGriff 7,630
1 Sergey Alexandrovich Kryukov 7,022
2 DamithSL 5,586
3 Manas Bhardwaj 4,946
4 Maciej Los 4,525


Advertise | Privacy | Mobile
Web03 | 2.8.1411023.1 | Last Updated 13 Dec 2012
Copyright © CodeProject, 1999-2014
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100