本文整理匯總了C#中System.Linq.Queryable.Max方法的典型用法代碼示例。如果您正苦於以下問題:C# Queryable.Max方法的具體用法?C# Queryable.Max怎麽用?C# Queryable.Max使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。
在下文中一共展示了Queryable.Max方法的4個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的C#代碼示例。
示例1: MaxEx2
class Pet
{
public string Name { get; set; }
public int Age { get; set; }
}
public static void MaxEx2()
{
Pet[] pets = { new Pet { Name="Barley", Age=8 },
new Pet { Name="Boots", Age=4 },
new Pet { Name="Whiskers", Age=1 } };
// Add Pet.Age to the length of Pet.Name
// to determine the "maximum" Pet object in the array.
int max =
pets.AsQueryable().Max(pet => pet.Age + pet.Name.Length);
Console.WriteLine(
"The maximum pet age plus name length is {0}.",
max);
}
輸出:
The maximum pet age plus name length is 14.
示例2:
List<long> longs = new List<long> { 4294967296L, 466855135L, 81125L };
long max = longs.AsQueryable().Max();
Console.WriteLine("The largest number is {0}.", max);
輸出:
The largest number is 4294967296.
示例3: Main
//引入命名空間
using System;
using System.Linq;
using System.Collections;
using System.Collections.Generic;
public class MainClass {
public static void Main() {
int[] myInts = new int[] { 974, 2, 7, 1374, 27, 54 };
int maxInt = myInts.Max();
Console.WriteLine(maxInt);
}
}
示例4: Queryable.Max(filter)
//引入命名空間
using System;
using System.Linq;
using System.Collections;
using System.Collections.Generic;
public class Employee {
public int birthYear;
public string firstName;
public string lastName;
public static Employee[] GetEmployees() {
Employee[] actors = new Employee[] {
new Employee { birthYear = 1964, firstName = "K", lastName = "R" },
new Employee { birthYear = 1968, firstName = "O", lastName = "W" },
new Employee { birthYear = 1960, firstName = "J", lastName = "S" },
new Employee { birthYear = 1964, firstName = "S", lastName = "B" },
};
return (actors);
}
}
public class MainClass {
public static void Main() {
int youngestEmployeeAge = Employee.GetEmployees().Max(a => a.birthYear);
Console.WriteLine(youngestEmployeeAge);
}
}