Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Linq Types

Posted on 2009-07-09
13
Medium Priority
?
336 Views
Last Modified: 2012-05-07
I am wondering about what type is returned when we do a Linq select.

Consider the attached code for discussion.

I know that the fortiesPeople in var fortiesPeople = from p in people where p.Age > 40 select p; returns an object which implements IEnumerable.  When I try and see what type it is though I get:

fortiesPeople type = System.Linq.Enumerable+WhereListIterator`1[LinqExamples.Person]

So what is all that - what is the + and the '1 etc

Thanks
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Collections;
 
namespace LinqExamples
{
    public class Program
    {
        static void Main(string[] args)
        {
            Person dave = new Person { FirstName = "Dave", LastName = "Amour", Age = 42 };
            Person fred = new Person { FirstName = "Fred", LastName = "Smith", Age = 22 };
            Person tiff = new Person { FirstName = "Tiff", LastName = "Bloggs", Age = 24 };
            Person kate = new Person { FirstName = "Kate", LastName = "Brown", Age = 33 };
            Person bill = new Person { FirstName = "Bill", LastName = "Green", Age = 38 };
            Person ian = new Person { FirstName = "Ian", LastName = "Smith", Age = 19 };
            Person fiona = new Person { FirstName = "Fiona", LastName = "Williams", Age = 28 };
            Person sonia = new Person { FirstName = "Sonia", LastName = "Johnson", Age = 43 };
            
            List<Person> people = new List<Person>();
 
            people.Add(dave);
            people.Add(fred);
            people.Add(tiff);
            people.Add(kate);
            people.Add(bill);
            people.Add(ian);
            people.Add(fiona);
            people.Add(sonia);
 
            var fortiesPeople = from p in people where p.Age > 40 select p;
 
            Console.WriteLine("fortiesPeople type = " + fortiesPeople.GetType().ToString());
 
            foreach (Person person in fortiesPeople)
            {
                Console.WriteLine(person);
            }
 
            Console.Read();
        }
    }
 
    public class Person
    {
        public string FirstName { get; set; }
        public string LastName { get; set; }
        public int Age { get; set; }
 
        public override string ToString()
        {
            return FirstName + " " + LastName + " (Age " + Age.ToString() + ")";
        }
    }
}

Open in new window

0
Comment
Question by:daveamour
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 7
  • 6
13 Comments
 
LVL 96

Expert Comment

by:Bob Learned
ID: 24818157
That is the signature for generics.  The class is defined like this:

private class WhereListIterator<TSource> : Enumerable.Iterator<TSource>

Enumerable = System.Linq.Enumerable
0
 
LVL 19

Author Comment

by:daveamour
ID: 24820855
Ok thanks but what is the + and the '1
Also it says is is System.Linq.Enumerable, not WhereListIterator.
Sorry still clueless.
 
0
 
LVL 96

Expert Comment

by:Bob Learned
ID: 24840428
Any time you see WhereListIterator`1, that means that it uses generics (WhereListIterator<TSource>).  The "+" symbol means that the first class is a base class (System.Linq.Enumerable), and the second class is the deriving class (WhereListIterator).

If you look at the class descriptor with Reflector, as shown above, you can see that WhereListIterator implements Enumerable.Iterator<TSource>, which is from the System.Linq.Enumerable class.
0
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 
LVL 19

Author Comment

by:daveamour
ID: 24846779
So the type is WhereListIterator which has a base class of System.Linq.Enumerable.
Where is the WhereListIterator class - I can't find it anywhere?
0
 
LVL 96

Expert Comment

by:Bob Learned
ID: 24849260
private class WhereListIterator

WhereListIterator is a private class in the framework, so you can't see it, but reflection can (I used Reflector to find it).
0
 
LVL 19

Author Comment

by:daveamour
ID: 24856813
Hmm if it's private then how come I can get an instance of it?  Confused.
0
 
LVL 96

Expert Comment

by:Bob Learned
ID: 24858452
Please explain, "I can get an instance of it".  Where are you creating an instance of a WhereListIterator?
0
 
LVL 19

Author Comment

by:daveamour
ID: 24858492
I am trying to find out what fortiesPeople is below
var fortiesPeople = from p in people where p.Age > 40 select p;
Did you not say it was a WhereListIterator or have I misunderstood?
 
0
 
LVL 96

Expert Comment

by:Bob Learned
ID: 24858538
"var fortiesPeople" uses type inference, which builds instances inside of the framework, that you can use to enumerate.  You are not creating an instance, the CLR is.  When you are checking the type, the CLR tells you what type it created.
0
 
LVL 19

Author Comment

by:daveamour
ID: 24858542
Ok so what type is it then?
0
 
LVL 96

Expert Comment

by:Bob Learned
ID: 24858562
WhereListIterator, which inherits from System.Linq.Enumerable, and lets you enumerate the objects.
0
 
LVL 19

Author Comment

by:daveamour
ID: 24858596
I don't understand how I can have an instance of a class (created by the CLR) which is of a private type.  Is it in fact expressed as an interface  - ie IEnumerbable - ie is there a method somewhere like this
public IEnumerable MyMethod()
 
0
 
LVL 96

Accepted Solution

by:
Bob Learned earned 1500 total points
ID: 24859234
Yes, you are getting that through an interface (IEnumerable<TSource>, IEnumerable, IEnumerator<TSource> or IEnumerator):

private abstract class Iterator<TSource> : IEnumerable<TSource>, IEnumerable, IEnumerator<TSource>, IDisposable, IEnumerator
0

Featured Post

Enroll in September's Course of the Month

This month’s featured course covers 16 hours of training in installation, management, and deployment of VMware vSphere virtualization environments. It's free for Premium Members, Team Accounts, and Qualified Experts!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Article by: Ivo
C# And Nullable Types Since 2.0 C# has Nullable(T) Generic Structure. The idea behind is to allow value type objects to have null values just like reference types have. This concerns scenarios where not all data sources have values (like a databa…
Performance in games development is paramount: every microsecond counts to be able to do everything in less than 33ms (aiming at 16ms). C# foreach statement is one of the worst performance killers, and here I explain why.
Video by: ITPro.TV
In this episode Don builds upon the troubleshooting techniques by demonstrating how to properly monitor a vSphere deployment to detect problems before they occur. He begins the show using tools found within the vSphere suite as ends the show demonst…
In this video you will find out how to export Office 365 mailboxes using the built in eDiscovery tool. Bear in mind that although this method might be useful in some cases, using PST files as Office 365 backup is troublesome in a long run (more on t…

722 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question