您当前的位置: 首页 > 
  • 4浏览

    0关注

    193博文

    0收益

  • 0浏览

    0点赞

    0打赏

    0留言

私信
关注
热门博文

LINQ之Min

我寄人间雪满头丶 发布时间:2020-05-29 22:12:23 ,浏览量:4

返回LINQ大全首页

Min()

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

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

public static decimal Min( this IEnumerable source ); public static double Min( this IEnumerable source ); public static float Min( this IEnumerable source ); public static int Min( this IEnumerable source ); public static long Min( 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     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( this IEnumerable source, Func selector ); public static double Min( this IEnumerable source, Func selector ); public static float Min( this IEnumerable source, Func selector ); public static int Min( this IEnumerable source, Func selector ); public static long Min( 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 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( this IEnumerable source, Func selector ); public static double? Min( this IEnumerable source, Func selector ); public static float? Min( this IEnumerable source, Func selector ); public static int? Min( this IEnumerable source, Func selector ); public static long? Min( this IEnumerable source, Func selector );

如果想要自定义类直接使用Min(),可以通过继承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 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( 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}", 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
关注
打赏
1648518768
查看更多评论
立即登录/注册

微信扫码登录

0.0870s