zoukankan      html  css  js  c++  java
  • LINQ查询表达式(5)

    查询表达式中处理Null值 

      此示例演示如何处理源集合中可能的 null 值。 诸如 IEnumerable<T> 等对象集合可能包含值为 null 的元素。 如果源集合为 null 或包含值为 null 的元素,并且查询未处理 null 值,当您执行查询时将会引发 NullReferenceException。

     var query1 =
                            from c in categories
                            where c != null
                            join p in products on c.ID equals
                                (p == null ? null : p.CategoryID)
                            select new { Category = c.Name, Name = p.Name };

       在 join 子句中,只要其中一个比较键是可以为 null 的类型,您就可以在查询表达式中将另一个比较键强制转换成可以为 null 的类型。 在下面的示例中,假定 EmployeeID 是一个列,其中包含类型为 int? 的值:

                void TestMethod(Northwind db)
                    {
                        var query =
                            from o in db.Orders
                            join e in db.Employees
                                on o.EmployeeID equals (int?)e.EmployeeID
                            select new { o.OrderID, e.FirstName };
                    }

     在查询表达式中处理异常

      下面的示例演示如何将异常处理代码移至查询表达式外部。 仅当该方法不依赖于查询的任何本地变量时,才能这样做。

       class ExceptionsOutsideQuery
        {
            static void Main()
            {
                // DO THIS with a datasource that might
                // throw an exception. It is easier to deal with
                // outside of the query expression.
                IEnumerable<int> dataSource;
                try
                {
                    dataSource = GetData();
                }
                catch (InvalidOperationException)
                {
                    // Handle (or don't handle) the exception 
                    // in the way that is appropriate for your application.
                    Console.WriteLine("Invalid operation");
                    goto Exit;
                }
                
                // If we get here, it is safe to proceed.
                var query = from i in dataSource
                            select i * i;
    
                foreach (var i in query)
                    Console.WriteLine(i.ToString());
    
                //Keep the console window open in debug mode
                Exit:
                Console.WriteLine("Press any key to exit");
                Console.ReadKey();
            }
    
            // A data source that is very likely to throw an exception!
            static IEnumerable<int> GetData()
            {
                throw new InvalidOperationException();
            }
        }

      在某些情况下,对在查询内引发的异常的最佳响应可能是立即停止执行查询。 请注意,try 块将 foreach 循环而不是查询本身封闭起来。 这是因为 foreach 循环是实际执行查询的场所。

    class QueryThatThrows
        {
            static void Main()
            {
                // Data source.
                string[] files = { "fileA.txt", "fileB.txt", "fileC.txt" };
    
                // Demonstration query that throws.
                var exceptionDemoQuery =
                    from file in files
                    let n = SomeMethodThatMightThrow(file)
                    select n;
    
                // Runtime exceptions are thrown when query is executed.
                // Therefore they must be handled in the foreach loop.
                try
                {
                    foreach (var item in exceptionDemoQuery)
                    {
                        Console.WriteLine("Processing {0}", item);
                    }
                }
    
                // Catch whatever exception you expect to raise
                // and/or do any necessary cleanup in a finally block
                catch (InvalidOperationException e)
                {
                    Console.WriteLine(e.Message);
                }
    
                //Keep the console window open in debug mode
                Console.WriteLine("Press any key to exit");
                Console.ReadKey();
            }
    
            // Not very useful as a general purpose method.
            static string SomeMethodThatMightThrow(string s)
            {
                if (s[4] == 'C')
                    throw new InvalidOperationException();
                return @"C:
    ewFolder" + s;
            }
        }
        /* Output:
            Processing C:
    ewFolderfileA.txt
            Processing C:
    ewFolderfileB.txt
            Operation is not valid due to the current state of the object.
         */

     参考

      [1] MSDN,查询表达式Null值处理

  • 相关阅读:
    Codeforces 1137E 凸包
    Codeforces 1142D Foreigner (DP)
    Codeforces 1120C Compress String(DP)
    gym
    Codeforces 1142B Lynyrd Skynyrd
    PTA 估值一亿的AI核心代码
    HDU 6162 树链剖分
    洛谷P2146 树链剖分
    HTTP深入浅出http请求(转)-----http请求的过程和实现机制
    css实现不定宽高的div水平、垂直居中
  • 原文地址:https://www.cnblogs.com/ybtools/p/6564394.html
Copyright © 2011-2022 走看看