这个Linq查询的含义是什么? [英] What is the meaning of this Linq Query?
本文介绍了这个Linq查询的含义是什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
Enum.GetValues(typeof(Gender)).Cast<Gender>().Select(c => new SelectListItem {
Text = c.ToString(),
Value = c.ToString()
}));
这里的性别是一个枚举。
请解释一下这句话每一步都在做什么。
谢谢
Here Gender is an Enum.
Please explain me what is this statement doing with each step by step.
Thanks
推荐答案
删除最后的close括号以使其编译...
Enum.GetValues返回枚举值的数组:so如果是enh enum:
Once you remove the final close bracket to get it to compile...
Enum.GetValues returns an array of the values in the enum: so if teh enum was:
public enum Gender
{
Male = 1,
Female = 2,
Bieber = 666,
}
然后它会返回一个ar三个整数值的光线:1,2,666
然后将它们转换为IEnumerable的Gender类项,并使用Select将每个项转换为新的SelectListItem实例,最后生成IEnumerable那些。
它相当于这段代码:
Then it would return an array of three integer values: 1, 2, 666
These it then casts to an IEnumerable of Gender class items, and uses Select to convert each of those into a new SelectListItem instance and finally results in an IEnumerable of those.
It's the equivalent of this code:
List<SelectListItem> list = new List<SelectListItem>();
foreach (int i in Enum.GetValues(typeof(Gender)))
{
Gender g = (Gender)i;
SelectListItem item = new SelectListItem();
item.Text = g.ToString();
item.Value = g.ToString();
list.Add(item);
}
IEnumerable<SelectListItem> returnValue = list;
这篇关于这个Linq查询的含义是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文