LINQ之Min

返回LINQ大全首页

Min()

使用Min()以获得序列中最小的元素。
MSDN

诸如int类型或float类型等使用Max()求最大值非常简单。

public static decimal Min( this IEnumerable<decimal> source );
public static double Min( this IEnumerable<double> source );
public static float Min( this IEnumerable<float> source );
public static int Min( this IEnumerable<int> source );
public static long Min( this IEnumerable<long> 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     intMin      = intNumbers.Min();
        float   floatMin    = floatNumbers.Min();

        System.Console.WriteLine( "intNumbers:{0}",   intNumbers.Text() );
        System.Console.WriteLine( "最小:{0}",         intMin );

        System.Console.WriteLine( "floatNumbers:{0}",   floatNumbers.Text() );
        System.Console.WriteLine( "最小:{0}",           floatMin );

        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],
最小:1
floatNumbers:[1], [3], [6],
最小:1

Min()支持数字类型为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?    intMin      = intNumbers.Min();
        float?  floatMin    = floatNumbers.Min();

        System.Console.WriteLine( "intNumbers:{0}",   intNumbers.Text() );
        System.Console.WriteLine( "最小:{0}",         intMin );

        System.Console.WriteLine( "floatNumbers:{0}",   floatNumbers.Text() );
        System.Console.WriteLine( "最小:{0}",           floatMin );

        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], ,
最小:1
floatNumbers:, , [],
最小:

对于非数字类型的自定义类型,我们可以通过设定条件进行对比。

public static decimal Min<TSource>( this IEnumerable<TSource> source, Func<TSource, decimal> selector );
public static double Min<TSource>( this IEnumerable<TSource> source, Func<TSource, double> selector );
public static float Min<TSource>( this IEnumerable<TSource> source, Func<TSource, float> selector );
public static int Min<TSource>( this IEnumerable<TSource> source, Func<TSource, int> selector );
public static long Min<TSource>( this IEnumerable<TSource> source, Func<TSource, long> selector );

public static class Program
{
    private class Parameter
    {
        public string   Name    { get; set; }
        public int      Age     { get; set; }
        
        public override string ToString()
        {
            return string.Format( "Name:{0}, Age:{1}", Name, Age );
        }
    }

    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 = "征史郎" },
        };

        int minAge = parameters.Min( value => value.Age );

        System.Console.WriteLine( "parameters:{0}", parameters.Text() );
        System.Console.WriteLine( "最小年齢:{0}", minAge );

        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],
最小年齢:18

Min()支持Nullable类型。

public static decimal? Min<TSource>( this IEnumerable<TSource> source, Func<TSource, decimal?> selector );
public static double? Min<TSource>( this IEnumerable<TSource> source, Func<TSource, double?> selector );
public static float? Min<TSource>( this IEnumerable<TSource> source, Func<TSource, float?> selector );
public static int? Min<TSource>( this IEnumerable<TSource> source, Func<TSource, int?> selector );
public static long? Min<TSource>( this IEnumerable<TSource> source, Func<TSource, long?> selector );

如果想要自定义类直接使用Min(),可以通过继承IComparable<T>实现其中的CompareTo自定义比较方法。
IComparable

public static class Program
{
    private class Parameter : System.IComparable<Parameter>
    {
        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 min   = parameters.Min();

        System.Console.WriteLine( "parameters:{0}", parameters.Text() );
        System.Console.WriteLine( "最小的人  :{0}", min );

        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

Min()也可以将元素中的其他自定义类进行比较。
public static TResult Min<TSource, TResult>( this IEnumerable<TSource> source, Func<TSource, TResult> 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<WeaponData>
    {
        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 比对方大于
        // <0 比对方小
        public int CompareTo( WeaponData i_other )
        {
            // 将Money的数量乘以Education的数值进行比较
            int thisNum     = Money * Education;
            int otherNum    = i_other.Money * i_other.Education;

            return thisNum - otherNum;
        }
    }

    static void Main( string[] args )
    {
        Parameter[] parameters = new Parameter[]
        {
            new Parameter() { Age = 52, Name = "正一郎", Weapon = new WeaponData() { Money = 100, Education = 1 } },
            new Parameter() { Age = 28, Name = "清次郎", Weapon = new WeaponData() { Money =  25, Education = 3 } },
            new Parameter() { Age = 20, Name = "誠三郎", Weapon = new WeaponData() { Money =  70, Education = 3 } },
            new Parameter() { Age = 18, Name = "征史郎", Weapon = new WeaponData() { Money =  10, Education = 5 } },
        };
        
        WeaponData minWeapon = parameters.Min( value => value.Weapon );
        System.Console.WriteLine( "parameters   :{0}", parameters.Text() );
        System.Console.WriteLine( "最脆弱的武器:{0}", minWeapon );

        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:10, Education:5
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

我寄人间雪满头丶

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值