Search code examples
linqprojection

what is a projection in LINQ, as in .Select()


I typically do mobile app development, which doesn't always have .Select. However, I've seen this used a bit, but I don't really know what it does or how it's doing whatever it does. It is anything like

    from a in list select a // a.Property // new Thing { a.Property}

I'm asking because when I've seen code using .Select(), I was a bit confused by what it was doing.


Solution

  • .Select() is from method syntax for LINQ, select in your code from a in list select a is for query syntax. Both are same, query syntax compiles into method syntax.

    You may see: Query Syntax and Method Syntax in LINQ (C#)

    Projection:

    Projection Operations - MSDN

    Projection refers to the operation of transforming an object into a new form that often consists only of those properties that will be subsequently used. By using projection, you can construct a new type that is built from each object. You can project a property and perform a mathematical function on it. You can also project the original object without changing it.

    You may also see: LINQ Projection

    The process of transforming the results of a query is called projection. You can project the results of a query after any filters have been applied to change the type of the collection that is returned.

    Example from MSDN

    List<string> words = new List<string>() { "an", "apple", "a", "day" };
    var query = from word in words
                select word.Substring(0, 1);
    

    In the above example only first character from each string instance is selected / projected.

    You can also select some fields from your collection and create an anonymous type or an instance of existing class, that process is called projection.

    from a in list select new { ID = a.Id}
    

    In the above code field Id is projected into an anonymous type ignoring other fields. Consider that your list has an object of type MyClass defined like:

    class MyClass
    {
        public int Id { get; set; }
        public string Name { get; set; }
        public string Address { get; set; }
    }
    

    Now you can project the Id and Name to an anonymous type like:

    Query Syntax:

    var result = from a in list
                 select new
                     {
                         ID = a.Id,
                         Name = a.Name,
                     };
    

    Method Syntax

    var result = list.Select(r => new { ID = r.Id, Name = r.Name });
    

    You can also project result to a new class. Consider you have a class like:

        class TemporaryHolderClass
        {
            public int Id { get; set; }
            public string Name { get; set; }
        }
    

    Then you can do:

    Query Syntax:

     var result = from a in list
                 select new TemporaryHolderClass
                     {
                         Id = a.Id,
                         Name = a.Name,
                     };
    

    Method Syntax:

    var result = list.Select(r => new TemporaryHolderClass  
                                { 
                                    Id = r.Id, 
                                    Name = r.Name 
                                });
    

    You can also project to the same class, provided you are not trying to project to classes generated/created for LINQ to SQL or Entity Framework.