查询表达式中处理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:\newFolder\" + s;
}
}
/* Output:
Processing C:\newFolder\fileA.txt
Processing C:\newFolder\fileB.txt
Operation is not valid due to the current state of the object.
*/

参考

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

最新文章

  1. 【代码笔记】iOS-截屏功能
  2. LeetCode(二)
  3. 【CodeForces 625A】Guest From the Past
  4. asp.net操作xml
  5. ecshop第一讲之安装
  6. 递归遍历XML节点属性和属性值
  7. Java正則表達式入门
  8. java的抽象类
  9. [置顶] 如何在Python IDLE中调试Python代码?
  10. JavaScript大厦之地基:js数据类型
  11. JAVA项目从运维部署到项目开发(一.Jenkins)
  12. EF 下如何更新数据表数据
  13. Qt编写自定义控件3-速度仪表盘
  14. &lt;mvc:annotation-driven /&gt; 作用
  15. RabbitMq基础教程之基本概念
  16. turple list dict 互相转换
  17. Uva821 Page Hopping (Floyd)
  18. Angular 4.x 修仙之路
  19. BZOJ 3391 [Usaco2004 Dec]Tree Cutting网络破坏(树形DP)
  20. ubtuntu 如何查看内存用量 mongostat详解

热门文章

  1. keystone 域中项目、用户、角色的创建
  2. jqGrid设置单选
  3. AOP+Token防止表单重复提交
  4. AOP实现事务和记录日志
  5. SQL——AND、OR运算符
  6. matlab 2017b 支持的C编译器
  7. JDBC第一个案例
  8. TCP 为什么需要三次握手而不是两次
  9. UML类图的几种关系总结
  10. 关于SqlServer数据库数据备份失败的问题