LINQ to Entities 中的已知问题和注意事项
本节提供有关 LINQ to Entities 查询的已知问题的信息。
不能缓存的 LINQ 查询
从 .NET Framework 4.5 开始,LINQ to Entities 查询是自动缓存的。 但是,不自动缓存将 Enumerable.Contains
运算符应用到内存中集合的 LINQ to Entities 查询。 此外,不允许在已编译的 LINQ 查询中参数化内存中的集合。
排序信息丢失
如果将列投影到匿名类型,则会导致对兼容级别设置为“80”的 SQL Server 2005 数据库所执行的某些查询中会丢失排序信息。 当 order-by 列表中的列名与选择器中的列名相同时,就会发生这种情况,如下面的示例所示:
using (AdventureWorksEntities context = new AdventureWorksEntities())
{
// Ordering information is lost when executed against a SQL Server 2005
// database running with a compatibility level of "80".
var results = context.Contacts.SelectMany(c => c.SalesOrderHeaders)
.OrderBy(c => c.SalesOrderDetails.Count)
.Select(c => new { c.SalesOrderDetails.Count });
foreach (var result in results)
Console.WriteLine(result.Count);
}
Using context As New AdventureWorksEntities()
' Ordering information is lost when executed against a SQL Server 2005
' database running with a compatibility level of "80".
Dim results = context.Contacts.SelectMany(Function(c) c.SalesOrderHeaders) _
.OrderBy(Function(c) c.SalesOrderDetails.Count) _
.Select(Function(c) New With {c.SalesOrderDetails.Count})
For Each result In results
Console.WriteLine(result.Count)
Next
End Using
不支持无符号整数
实体框架不支持无符号整数,因此不支持在 LINQ to Entities 查询中指定无符号整数类型。 如果指定无符号整数,则在查询表达式转换过程中会引发 ArgumentException 异常,如下面的示例所示。 此示例查询其 ID 为 48000 的订单。
using (AdventureWorksEntities context = new AdventureWorksEntities())
{
uint s = UInt32.Parse("48000");
IQueryable<SalesOrderDetail> query = from sale in context.SalesOrderDetails
where sale.SalesOrderID == s
select sale;
// NotSupportedException exception is thrown here.
try
{
foreach (SalesOrderDetail order in query)
Console.WriteLine("SalesOrderID: " + order.SalesOrderID);
}
catch (NotSupportedException ex)
{
Console.WriteLine("Exception: {0}", ex.Message);
}
}
Using context As New AdventureWorksEntities()
Dim saleId As UInteger = UInt32.Parse("48000")
Dim query = _
From sale In context.SalesOrderDetails _
Where sale.SalesOrderID = saleId _
Select sale
Try
' NotSupportedException exception is thrown here.
For Each order As SalesOrderDetail In query
Console.WriteLine("SalesOrderID: " & order.SalesOrderID)
Next
Catch ex As NotSupportedException
Console.WriteLine("Exception: " + ex.Message)
End Try
End Using
类型转换错误
在 Visual Basic 中,如果使用 CByte
函数将某一属性映射到值为 1 且为 SQL Server 位类型的列,则会引发 SqlException 并显示“发生算术溢出错误”消息。 下面的示例查询 AdventureWorks 示例数据库中的 Product.MakeFlag
列,在循环访问查询结果时引发一个异常。
Using context As New AdventureWorksEntities()
Dim productsList = _
From product In context.Products _
Select CByte(product.MakeFlag)
' Throws an SqlException exception with a "Arithmetic overflow error
' for data type tinyint" message when a value of 1 is iterated over.
For Each makeFlag In productsList
Console.WriteLine(makeFlag)
Next
End Using
不支持引用非标量变量
不支持在查询中引用非标量变量(如实体)。 在此类查询执行时,会引发 NotSupportedException 异常,并显示以下消息:“无法创建类型为‘EntityType
’的常量值。 此上下文中仅支持基元类型(‘如 Int32、String 和 Guid’)。”[Unable to create a constant value of type 'Closure type'. Only primitive types ('such as Int32, String, and Guid') are supported in this context.]
注意
支持引用标量变量的集合。
using (AdventureWorksEntities context = new AdventureWorksEntities())
{
Contact contact = context.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 context.Contacts
where c == contact
select c.LastName;
try
{
foreach (string name in contacts)
{
Console.WriteLine("Name: ", name);
}
}
catch (NotSupportedException ex)
{
Console.WriteLine(ex.Message);
}
}
Using context As New AdventureWorksEntities()
Dim contact As Contact = context.Contacts.FirstOrDefault()
' Referencing a non-scalar closure in a query will
' throw an exception when the query is executed.
Dim contacts = From c In context.Contacts _
Where c.Equals(contact) _
Select c.LastName
Try
For Each name As String In contacts
Console.WriteLine("Name: ", name)
Next
Catch ex As Exception
Console.WriteLine(ex.Message)
End Try
End Using
使用 SQL Server 2000,嵌套查询可能会失败
使用 SQL Server 2000,如果 LINQ to Entities 查询生成具有三级或以上深度的嵌套 Transact-SQL 查询,则它们可能会失败。
投影到匿名类型
如果通过使用 Include 上的 ObjectQuery<T> 方法将初始查询路径定义为包括相关对象,然后使用 LINQ 将返回的对象投影到匿名类型,则查询结果中将不包含在 Include 方法中指定的对象。
using (AdventureWorksEntities context = new AdventureWorksEntities())
{
var resultWithoutRelatedObjects =
context.Contacts.Include("SalesOrderHeaders").Select(c => new { c }).FirstOrDefault();
if (resultWithoutRelatedObjects.c.SalesOrderHeaders.Count == 0)
{
Console.WriteLine("No orders are included.");
}
}
Using context As New AdventureWorksEntities()
Dim resultWithoutRelatedObjects = context.Contacts. _
Include("SalesOrderHeaders"). _
Select(Function(c) New With {c}).FirstOrDefault()
If resultWithoutRelatedObjects.c.SalesOrderHeaders.Count = 0 Then
Console.WriteLine("No orders are included.")
End If
End Using
若要获取相关对象,请勿将返回的类型投影到匿名类型。
using (AdventureWorksEntities context = new AdventureWorksEntities())
{
var resultWithRelatedObjects =
context.Contacts.Include("SalesOrderHeaders").Select(c => c).FirstOrDefault();
if (resultWithRelatedObjects.SalesOrderHeaders.Count != 0)
{
Console.WriteLine("Orders are included.");
}
}
Using context As New AdventureWorksEntities()
Dim resultWithRelatedObjects = context.Contacts. _
Include("SalesOrderHeaders"). _
Select(Function(c) c).FirstOrDefault()
If resultWithRelatedObjects.SalesOrderHeaders.Count <> 0 Then
Console.WriteLine("Orders are included.")
End If
End Using