What is projection in LINQ, as in .Select ()

I usually develop mobile apps that I don’t always have. Select. However, I saw that it got a little messed up, but I really don’t know what he does and how he does everything he does. This is something like

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

I ask, because when I saw the code using .Select (), I was a little confused about what it was doing.

+4
source share
2 answers

.Select()is the method syntax for LINQ, selectin your code from a in list select afor the query syntax. Both are the same, the query syntax is compiled into the method syntax.

You can see: Query syntax and method syntax in LINQ (C #)

Projection:

- MSDN

, , . , . . , .

: LINQ

. , .

MSDN

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

/ .

, .

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

Id , . , MyClass, :

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

Id Name , :

:

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

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

. :

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

:

:

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

:

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

, , / LINQ to SQL Entity Framework.

+15

- ( ) .

select "" . , , , - , .

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

, , , - Name - Name. , - () / .

+1

Source: https://habr.com/ru/post/1537766/


All Articles