LINQ中的投影是什么,如.Select() [英] what is a projection in LINQ, as in .Select()

查看:94
本文介绍了LINQ中的投影是什么,如.Select()的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我通常进行移动应用程序开发,但并不总是具有.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}

我之所以问是因为,当我看到使用.Select()的代码时,我对它的工作感到有些困惑.

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

推荐答案

.Select()来自LINQ的方法语法,代码from a in list select a中的select用于查询语法.两者相同,查询语法会编译为方法语法.

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

您可能会看到: LINQ(C#)中的查询语法和方法语法

投影:

投影操作-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.

您可能还会看到: LINQ投影

转换查询结果的过程称为 投影.您可以在任何过滤器之后投影查询结果 已应用于更改集合的类型,即 返回.

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.

来自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}

在上面的代码字段中,Id被投影为匿名类型,而忽略了其他字段.考虑您的列表中有一个定义为MyClass的对象,如下所示:

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; }
}

现在,您可以将IdName投影为匿名类型,例如:

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

查询语法:

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 });

您还可以将结果投影到新的班级.考虑您有一个像这样的课程:

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:

查询语法:

 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生成/创建的类,则还可以投影到同一类.

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.

这篇关于LINQ中的投影是什么,如.Select()的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆