我需要一些帮助来理解这段代码的作用。 [英] I need some help in understanding what this code does.

查看:74
本文介绍了我需要一些帮助来理解这段代码的作用。的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

GridView1.DataSource = Enumerable.Range(1,5).Select(a => new

{



FirstName = String.Format(名字{0},a),

LastName = String.Format姓氏{0},a),

ShowButton = a .Equals(5)

});

GridView1.DataBind();

解决方案

Enumerable.Range 返回来自给定起始值的计数值的集合。所以在你的情况下它会创建一个包含五个值的集合:1,2,3,4,5

然后对于每个值,创建一个具有三个属性的新匿名数据类型:FirstName,LastName,和ShowButton,为前两个分配适当的字符串,并将bool分配给最后一个。在每个新实例中,集合中的单个值将作为a传递,因此每个字符串值将不同,并且只有最终的bool才为真。最后它返回一组新实例。

此集合被设置为gridview的数据源,数据将显示为五行三列。



我引用返回的原因是因为它没有 - 它直到最后一秒实际需要时才生成任何值。但是你可以把它当作返回一个价值 - Linq的内部工作原理并不是像这样的小文本框的主题! :笑:

GridView1.DataSource = Enumerable.Range(1, 5).Select(a => new
{

FirstName = String.Format("First Name {0}", a),
LastName = String.Format"Last Name {0}", a),
ShowButton = a.Equals(5)
});
GridView1.DataBind();

解决方案

Enumerable.Range "returns" a collection of count values from the given start value. So in your case it creates an collection of five values: 1, 2, 3, 4, 5.
Then for each of those, is creates a new anonymous datatype with three properties: FirstName, LastName, and ShowButton, assigning the appropriate string to the first two, and a bool to the last. In each new instance, a single value from the collection will be passed as "a" so each of the string values will be different, and only the final bool will be true. At the end it "returns" a collection of the new instances.
This collection is set as the datasource for the gridview, and the data will be displayed as five rows of three columns.

The reason I'm quoting "returns" is because it doesn't - it doesn't generate any values until the last second when they are actually required. But you can think of it as "returning" a value for the moment - the inner workings of Linq are not a subject for a little text box like this! :laugh:


这篇关于我需要一些帮助来理解这段代码的作用。的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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