Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
2.5k views
in Technique[技术] by (71.8m points)

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.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...