返回LINQ大全首页
Max()使用Max()
以获得序列中最大的元素。 MSDN
诸如int
类型或float
类型等使用Max()求最大值非常简单。
public static decimal Max( this IEnumerable source );
public static double Max( this IEnumerable source );
public static float Max( this IEnumerable source );
public static int Max( this IEnumerable source );
public static long Max( this IEnumerable source );
代码示例:
public static class Program
{
static void Main( string[] args )
{
int[] intNumbers = new int[] { 1, 4, 3, 4 };
float[] floatNumbers = new float[] { 1.0f, 3.0f, 6.0f };
int intMax = intNumbers.Max();
float floatMax = floatNumbers.Max();
System.Console.WriteLine( "intNumbers:{0}", intNumbers.Text() );
System.Console.WriteLine( "最大:{0}", intMax );
System.Console.WriteLine( "floatNumbers:{0}", floatNumbers.Text() );
System.Console.WriteLine( "最大:{0}", floatMax );
System.Console.ReadKey();
}
public static string Text( this IEnumerable i_source )
{
string text = string.Empty;
foreach( var value in i_source )
{
text += string.Format( "[{0}], ", value );
}
return text;
}
}
intNumbers:[1], [4], [3], [4],
最大:4
floatNumbers:[1], [3], [6],
最大:6
Max()
支持数字类型为null
。
代码示例:
public static class Program
{
static void Main( string[] args )
{
int?[] intNumbers = new int?[] { 1, null, 3, null };
float?[] floatNumbers = new float?[] { null, null, null };
int? intMax = intNumbers.Max();
float? floatMax = floatNumbers.Max();
System.Console.WriteLine( "intNumbers:{0}", intNumbers.Text() );
System.Console.WriteLine( "最大:{0}", intMax );
System.Console.WriteLine( "floatNumbers:{0}", floatNumbers.Text() );
System.Console.WriteLine( "最大:{0}", floatMax );
System.Console.ReadKey();
}
public static string Text( this IEnumerable i_source )
{
string text = string.Empty;
foreach( var value in i_source )
{
text += string.Format( "[{0}], ", value );
}
return text;
}
}
intNumbers:[1], , [3], ,
最大:3
floatNumbers:, , [],
最大:
对于非数字类型的自定义类型,我们可以通过设定条件进行对比。
public static decimal Max( this IEnumerable source, Func selector );
public static double Max( this IEnumerable source, Func selector );
public static float Max( this IEnumerable source, Func selector );
public static int Max( this IEnumerable source, Func selector );
public static long Max( this IEnumerable source, Func selector );
代码示例:
public static class Program
{
private class Parameter
{
public string Name { get; set; }
public int Age { get; set; }
public override string
{
return string.Format( "Name:{0}, Age:{1}"
}
}
static void Main( string
{
Parameter[] parameters = new
{
new Parameter() { Age = 52, Name = "正一郎" },
new Parameter() { Age = 28, Name = "清次郎" },
new Parameter() { Age = 20, Name = "誠三郎" },
new Parameter() { Age = 18, Name = "征史郎" },
};
int maxAge = parameters.Max( value => value.Age );
System.Console.WriteLine( "parameters:{0}", parameters.Text() );
System.Console.WriteLine( "最大年龄:{0}", maxAge );
System.Console.ReadKey();
}
public static string Text( this IEnumerable i_source )
{
string text = string.Empty;
foreach( var value in i_source )
{
text += string.Format( "[{0}], ", value );
}
return text;
}
}
parameters:[Name:正一郎, Age:52], [Name:清次郎, Age:28], [Name:誠三郎, Age:20],
[Name:征史郎, Age:18],
最大年龄:52
如果想要自定义类直接使用Max()
,可以通过继承IComparable
实现其中的CompareTo
自定义比较方法。 IComparable
代码示例:
public static class Program
{
private class Parameter : System.IComparable
{
public string Name { get; set; }
public int Age { get; set; }
public override string ToString()
{
return string.Format( "Name:{0}, Age:{1}", Name, Age );
}
// 实现接口中的比较方法
// 0 相等
// -1 小于目标
// 1 大于目标
public int CompareTo( Parameter i_other )
{
int thisNum = this.Age + this.Name.Length;
int otherNum = i_other.Age + i_other.Name.Length;
if( thisNum == otherNum )
{
return 0;
}
if( thisNum > otherNum )
{
return 1;
}
return -1;
}
}
static void Main( string[] args )
{
Parameter[] parameters = new Parameter[]
{
new Parameter() { Age = 52, Name = "正一郎" },
new Parameter() { Age = 28, Name = "清次郎" },
new Parameter() { Age = 20, Name = "誠三郎" },
new Parameter() { Age = 18, Name = "征史郎" },
};
Parameter max = parameters.Max();
System.Console.WriteLine( "parameters:{0}", parameters.Text() );
System.Console.WriteLine( "最大的人 :{0}", max );
System.Console.ReadKey();
}
public static string Text( this IEnumerable i_source )
{
string text = string.Empty;
foreach( var value in i_source )
{
text += string.Format( "[{0}], ", value );
}
return text;
}
}
parameters:[Name:正一郎, Age:52], [Name:清次郎, Age:28], [Name:誠三郎, Age:20],
[Name:征史郎, Age:18],
最大的人 :Name:正一郎, Age:52
Max()也可以将元素中的其他自定义类进行比较。 public static TResult Max( this IEnumerable source, Func selector );
代码示例:
public static class Program
{
private class Parameter
{
public string Name { get; set; }
public int Age { get; set; }
public WeaponData Weapon { get; set; }
public override string ToString()
{
return string.Format( "Name:{0}, Age:{1}, {2}", Name, Age, Weapon );
}
}
private class WeaponData : System.IComparable
{
public int Money { get; set; }
public int Education { get; set; }
public override string ToString()
{
return string.Format( "Money:{0}, Education:{1}", Money, Education );
}
// 自定义比较方法
// 0 相等
// >0 比对方大于
// value.Weapon );
System.Console.WriteLine( "parameters :{0}", parameters.Text() );
System.Console.WriteLine( "最强的武器:{0}", maxWeapon );
System.Console.ReadKey();
}
public static string Text( this IEnumerable i_source )
{
string text = string.Empty;
foreach( var value in i_source )
{
text += string.Format( "[{0}], ", value );
}
return text;
}
}
parameters :[Name:正一郎, Age:52, Money:100, Education:1], [Name:清次郎, Age:2
8, Money:25, Education:3], [Name:誠三郎, Age:20, Money:70, Education:3], [Name:
征史郎, Age:18, Money:10, Education:5],
最强的武器:Money:70, Education:3