代码编写优化

时间:2013-04-05 07:20:14

标签: .net vb.net

我有一个上课的人,有身份证,姓名和工资属性。

class person
    friend id as long
    friend name as string
    friend salary as decimal
end class

我不想列出工资清单。

Dim pList as new list(of person)

pList加载了50个条目

Dim SalaryList as new list(of decimal)
for each p as person in plist
    SalaryList.add(p.salary)
next

好的,这很有效。 但有没有办法像这样做:

Dim SalaryList as list(of decimal) = pList(each).salary

1 个答案:

答案 0 :(得分:1)

您可以使用Select投影:

Dim SalaryList = pList.Select(Function(p) p.salary).ToList()

Dim SalaryList = From p in pList
                 Select p.salary

请注意,后者只返回查询(IEnumerable(Of Decimal)),而不是List。如果您确实需要List,请在查询中调用ToList(),但通常没有必要。