.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