不支持在查询中引用非标量变量(如实体)。在此类查询执行时,会引发 NotSupportedException 异常,并显示以下消息:“无法创建类型为‘EntityType’的常量值。此上下文中仅支持基元类型(‘如 Int32、String 和 Guid’)。”[Unable to create a constantvalue of type 'Closure type'. Only primitive types ('such as Int32,String, and Guid') are supported in this context.]
![]() |
---|
支持引用标量变量的集合。 |
using (AdventureWorksEntities AWEntities = new AdventureWorksEntities())
{
Contact contact = AWEntities.Contacts.FirstOrDefault();
// Referencing a non-scalar closure in a query will
// throw an exception when the query is executed.
IQueryable<string> contacts = from c in AWEntities.Contacts
where c == contact
select c.LastName;
try
{
foreach (string name in contacts)
{
Console.WriteLine("Name: ", name);
}
}
catch (NotSupportedException ex)
{
Console.WriteLine(ex.Message);
}
}
就是说你出现在 where 中的这些变量必须是原始类型变量,不可以是你自定义的类或结构。
非标量 英文为 Non-Scalar,意思是不能标注数量的。原始类型比如 int ,.net framework 可以比较其大小,数量,但如果你自定义一个类,用 == , > 这些比较操作符,.net framework 就无法进行比较了。
在SQL2008的查询优化架构中,有几个非常重要的概念:
统计(Statistics)、标量评估(Cardinality estimation)和成本(costing)
本人正整理这一部分的读书笔记,敬请关注:
http://www.cnblogs.com/downmoon/archive/2010/06/06/1742318.html
也可以在msdn查找:
http://msdn.microsoft.com/en-us/library/ms175933.aspx