15

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.

0

3 Answers 3

40

.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.

0
1

My summary is it takes results (or a subset of results) and allows you to quickly restructure it for use in the local context.

The select clause produces the results of the query and specifies the "shape" or type of each returned element. For example, you can specify whether your results will consist of complete Customer objects, just one member, a subset of members, or some completely different result type based on a computation or new object creation.

Source: http://msdn.microsoft.com/en-us/library/bb397927.aspx

There are a lot of possible uses for this but one is taking a complex object which of many other contains a property that is a string -- say Name -- and allows you to return an enumeration with just the entries of Name. I believe you can also do the opposite -- use that property ( for example) and create / return new type of object while passing in a property or properties.

0
-1

It means "mapping". Map each element of a sequence to a transformed sequence. I hadn't comprehended its meaning before I looked at the image.

Where does the meaning of the word come from?

Simply, math! https://mathworld.wolfram.com/Projection.html

enter image description here

2
  • It seems you missed the question is about LINQ projection.
    – CLS
    Sep 9, 2021 at 7:56
  • 1
    @CLS no i m pretty sure Sep 9, 2021 at 7:57

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.