Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/.net/24.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/vb.net/17.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
.net 代码编写优化_.net_Vb.net - Fatal编程技术网

.net 代码编写优化

.net 代码编写优化,.net,vb.net,.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

我有一个班级成员,身份证,姓名,薪资属性

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
可以使用投影:

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


请注意,后者只返回一个查询(
IEnumerable(十进制)
),而不是
列表。如果你真的需要一个
列表
,调用查询,但通常不需要。

你想优化哪一部分?到“灰色代码”:也许你应该完整地阅读它。最后一个问题是如何在一行中而不是在一个有趣的答案中实现它,但是Select接口没有在plist(.net 3.5)上实现,我不能使用Linq为什么不能使用Linq?它是.NET3.5的一部分,所以没有理由不使用它。要使用
Select
-方法,您需要引用
System.Core
并导入
System.Linq
命名空间。否则,就没有办法将代码放在一行中。我一直认为linq会降低性能,但你说服了我。我会对它进行测试。@cyrianox-您是否进行了测量,表明使用LINQ编写的代码存在一致的性能问题?
Dim SalaryList = From p in pList
                 Select p.salary