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

    0关注

    193博文

    0收益

  • 0浏览

    0点赞

    0打赏

    0留言

私信
关注
热门博文

LINQ之Single,SingleOrDefault

我寄人间雪满头丶 发布时间:2020-04-13 19:22:29 ,浏览量:3

返回LINQ大全首页

目录
  • Single()
  • SingleOrDefault()

Single()

Single()的用法是获取唯一的元素。 public static TSource Single( this IEnumerable source ); MSDN 听起来可能有点抽象,请继续往下看。

代码示例:

public static class Program
{
    static void Main( string[] args )
    {
        int[] numbers = new int[] { 5 };

        int result  = numbers.Single();

        System.Console.WriteLine( "数据:{0}", numbers.Text() );
        System.Console.WriteLine( "结果:{0}", result );

        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;
    }
} 

数据:[5],
结果:5

通过single()我们获取了数组中唯一的5元素。 不止如此,我们还可以指定获取的条件。

示例代码:

public static class Program
{
    static void Main( string[] args )
    {
        int[] numbers = new int[] { 1, 2, 3, 5, 7, 11 };

        // 大于10的值
        int result  = numbers.Single( value => value > 10 );

        System.Console.WriteLine( "数据:{0}", numbers.Text() );
        System.Console.WriteLine( "结果:{0}", result );

        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;
    }
} 
数据:[1], [2], [3], [5], [7], [11],
结果:11

Single()可以处理任何类型。 因此,无论是float,还是string,它都能处理任何事情。 以下是使用class的数组的示例。

示例代码:

public static class Program
{
    private class Parameter
    {
        public int      ID      { get; set; }
        public string   Name    { get; set; }

        public override string ToString()
        {
            return string.Format( "ID:{0}, Name:{1}", ID, Name );
        }
    }

    static void Main( string[] args )
    {
        Parameter[] parameters = new Parameter[]
        {
            new Parameter() { ID =  5, Name = "正一郎" },
            new Parameter() { ID = 13, Name = "清次郎" },
            new Parameter() { ID = 25, Name = "誠三郎" },
            new Parameter() { ID = 42, Name = "征史郎" },
        };

        // ID小于10
        Parameter result  = parameters.Single( value => value.ID  value > 20 );
        }
        catch( System.Exception i_exception )
        {
            System.Console.WriteLine( "异常:{0}", i_exception );

            System.Console.ReadKey();
            return;
        }  

        System.Console.WriteLine( "数据:{0}", numbers.Text() );
        System.Console.WriteLine( "结果:{0}", result );

        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;
    }
} 

数据:[1], [2], [3], [5], [7], [11],
结果:0

但是,使用SingleOrDefault()要小心,因为可能会无法区分“找不到元素,则返回默认值0”还是“找到元素0,所以我返回了它。”

还要注意一点的是,如果获取到多个元素,则会引发异常!

代码示例:

public static class Program
{
    static void Main( string[] args )
    {
        int[] numbers = new int[] { 1, 2, 3, 5, 7, 11 };
        int result = 0;
        try
        {
            // 获取奇数
            result = numbers.SingleOrDefault( value => value % 2 == 1 );
        }
        catch( System.Exception i_exception )
        {
            System.Console.WriteLine( "异常:{0}", i_exception );

            System.Console.ReadKey();
            return;
        }  

        System.Console.WriteLine( "数据:{0}", numbers.Text() );
        System.Console.WriteLine( "结果:{0}", result );

        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;
    }
} 

异常:System.InvalidOperation Exception:序列中包含多个匹配的元素
已完成

所以,Single()SingleOrDefault()是用来获取唯一元素的! 不应在获取多个元素情况下使用它!

关注
打赏
1648518768
查看更多评论
立即登录/注册

微信扫码登录

0.1896s