RSS
热门关键字:
当前位置 :| 主页>net development>

The LINQ in Visual Basic and C# is aggregate

来源: 作者:Stand originally 时间:2008-12-21 Tag: 点击:
Aggregate is one can get the function that scalar quantity is worth from inside gather of a data, the Min() with for instance medium T-SQL, Max() and Sum() . The function with VB and aggregate to this kind also now C# gives at support, but be with a kind of very different kind.

VB and C# support group with the form of patulous method. In an IEnumberable object, be being called simply is finished through nodding a symbol, for instance:
Var TotalVirtualMemory = (from P In Process.GetProcesses()
Select P.VirtualMemorySize64).Sum();

Dim TotalVirtualMemory = _
(From P In Process.GetProcesses _
Select P.VirtualMemorySize64).Sum


From here can see, the version of VB and C# is same almost. But VB still offerred grammar of a LINQ technically for polymerization:

Dim TotalVirtualMemory = Aggregate P In Process.GetProcesses _ Into P.VirtualMemorySize64
  
If this is only area is other between both word, it whats do not have so is good to whats do not have so talk. But, interesting thing happens in think when you the operation is more than at the same time when a “ lists ” . Handy for the purpose of, all and fictitious memory that we want an operation to be being used suppose and complete job market (physical memory) .

Use faceless kind, we found easily to contain the variable that they are worth twice.
Var Totals = New {
TotalVirtualMemory = (from P In Process.GetProcesses() Select P.VirtualMemorySize64).Sum() ,
TotalWorkingSet = (from P In Process.GetProcesses() Select P.WorkingSet64).Sum() };
 
The problem here is GetProcesses() was called twice. That is to say the operating system must inquire twice, the loop is carried out twice in assembling as a result. A swifter method perhaps is pair of GetProcesses() call have cache.
Var Processes = (from P In Process.GetProcesses() Select New {P.VirtualMemorySize64, p.WorkingSet64} ).ToList();Var Totals2 = New {TotalVirtualMemory = (from P In Processes Select P.VirtualMemorySize64).Sum() , TotalWorkingSet = (from P In Processes Select P.WorkingSet64).Sum()
};
  
Although a few better, but still need twice to circulate. How be carried out only? At this moment we need a custom-built group implement, save these results with name kind (Named Class) .
Public Static ProcessTotals Sum(this IEnumerable Source) {Var Totals = New ProcessTotals();Foreach (var P In Source){Totals.VirtualMemorySize64 = P.VirtualMemorySize64;Totals.WorkingSet64 = P.WorkingSet64;
}Return Totals;
}Public Class ProcessTotals {Public Long VirtualMemorySize64 {Get; Set; }Public Long WorkingSet64 {Get; Set; }
}Var Totals3 = (from P In Process.GetProcesses() Select P).Sum();

Developer is in such OK also doing in Visual Basic, but do so below need resembling:

Dim Totals3 = Aggregate P In Process.GetProcesses _
Into VirtualMemory = Sum(p.VirtualMemorySize64) , _WorkingSet = Sum(p.WorkingSet64)
最新评论共有 0 位网友发表了评论
发表评论
评论内容:不能超过250字,需审核,请自觉遵守互联网相关政策法规。
用户名: 密码:
匿名?
注册