|
Este artículo proviene de un motor de traducción automática. Mueva el puntero sobre las frases del artículo para ver el texto original. Más información.
|
Traducción
Original
|
Aggregate (Cláusula, Visual Basic)
Aggregate element [As type] In collection _ [, element2 [As type2] In collection2, [...]] [ clause ] Into expressionList
|
|
|
|
element |
|
|
type |
|
|
collection |
|
|
clause |
|
|
expressionList |
|
|
|
|
|
All |
Dim customerList1 = Aggregate order In orders Into AllOrdersOver100 = All(order.Total >= 100) |
|
Any |
Dim customerList2 = From cust In customers Aggregate order In cust.Orders Into AnyOrderOver500 = Any(order.Total >= 500) |
|
Average |
Dim customerOrderAverage = Aggregate order In orders Into Average(order.Total) |
|
Count |
Dim customerOrderAfter1996 = From cust In customers Aggregate order In cust.Orders Into Count(order.OrderDate > #12/31/1996#) |
|
Group |
|
|
LongCount |
|
|
Max |
Dim customerMaxOrder = Aggregate order In orders Into MaxOrder = Max(order.Total) |
|
Min |
Dim customerMinOrder = From cust In customers Aggregate order In cust.Orders Into MinOrder = Min(order.Total) |
|
Sum |
Dim customerTotals = From cust In customers Aggregate order In cust.Orders Into Sum(order.Total) |
Public Sub AggregateSample() Dim customers = GetCustomerList() Dim customerOrderTotal = From cust In customers Aggregate order In cust.Orders Into Sum(order.Total), MaxOrder = Max(order.Total), MinOrder = Min(order.Total), Avg = Average(order.Total) For Each customer In customerOrderTotal Console.WriteLine(customer.cust.CompanyName & vbCrLf & vbTab & "Sum = " & customer.Sum & vbCrLf & vbTab & "Min = " & customer.MinOrder & vbCrLf & vbTab & "Max = " & customer.MaxOrder & vbCrLf & vbTab & "Avg = " & customer.Avg.ToString("#.##")) Next End Sub
Imports System.Runtime.CompilerServices Module UserDefinedAggregates ' Calculate the median value for a collection of type Double. <Extension()> Function Median(ByVal values As IEnumerable(Of Double)) As Double If values.Count = 0 Then Throw New InvalidOperationException("Cannot compute median for an empty set.") End If Dim sortedList = From number In values Order By number Dim medianValue As Double Dim itemIndex = CInt(Int(sortedList.Count / 2)) If sortedList.Count Mod 2 = 0 Then ' Even number of items in list. medianValue = ((sortedList(itemIndex) + sortedList(itemIndex - 1)) / 2) Else ' Odd number of items in list. medianValue = sortedList(itemIndex) End If Return medianValue End Function ' "Cast" the collection of generic items as type Double and call the ' Median() method to calculate the median value. <Extension()> Function Median(Of T)(ByVal values As IEnumerable(Of T), ByVal selector As Func(Of T, Double)) As Double Return (From element In values Select selector(element)).Median() End Function End Module
Module Module1 Sub Main() Dim numbers1 = {1, 2, 3, 4, 5} Dim query1 = Aggregate num In numbers1 Into Median(num) Console.WriteLine("Median = " & query1) Dim numbers2 = {1.9, 2, 8, 4, 5.7, 6, 7.2, 0} Dim query2 = Aggregate num In numbers2 Into Median() Console.WriteLine("Median = " & query2) End Sub End Module