【发布时间】:2013-11-25 00:12:13
【问题描述】:
我对 LINQ-to-Entities 比较陌生,但经常使用 LINQ-to-SQL。
我正在使用带有实体框架 6 和 MVC 5 的 Visual Studio 2013。
两者之间的最大区别在于,Linq2SQL 能够在 SELECT 查询本身内部执行转换,而 LINQ2Entities 则不那么宽容,必须在执行 LINQ 查询之前进行正确的转换。因此,我收到错误:
“BillYeagerDB.EdmxExtensionMethods”类型上的指定方法“System.Decimal ConvertToDecimal(Byte)”无法转换为 LINQ to Entities 存储表达式。
经过大量研究,尤其是在 Stack Overflow 上的这个问题,我发现了一个链接 (LINQ to Entities does not recognize the method 'Double Parse(System.String)' method, and this method cannot be translated into a store expression),但他正在使用 ObjectContext,而我正在使用 DbContext。
我也确信它对我有用,但我认为我只是错误地设计了扩展方法(这给了我上述错误)。请注意,此特定问题与 LINQ 查询中的 AvgRating 变量有关。一旦我可以让它工作,我可以对任何其他转换进行相同类型的修复。注意AvgRating 定义为Decimal 类型,a.Rating.RatingValue 定义为Byte 类型。
如果有人能帮我解决这个问题,我将不胜感激。
这是我的代码。我正在尝试使用以下查询,但由于转换问题,我知道它不会起作用(如前所述)。
原始 LINQ 查询:
namespace BillYeagerDB
{
public class BillYeagerDB
{
public async Task<List<RestaurantList>> GetRestaurantListAsync()
{
try
{
using (BillYeagerEntities DbContext = new BillYeagerEntities())
{
DbContext.Database.Connection.Open();
var restaurants = await DbContext.Restaurants.GroupBy(g => g).Select(s =>
new RestaurantList()
{
Name = s.Key.Name,
City = s.Key.City,
Phone = s.Key.Phone,
AvgRating = s.Average(a => Convert.ToDecimal(a.Rating.RatingValue)),
NbrOfPeopleRating = s.Distinct().Count(c => Convert.ToBoolean(c.RatingId)),
Id = s.Key.Id
}).ToListAsync();
return restaurants;
}
}
catch (Exception)
{
throw;
}
}
}
}
我需要更新我的 EDMX 文件
<edmx:ConceptualModels>
<Schema Namespace="BillYeagerModel" Alias="Self" annotation:UseStrongSpatialTypes="false" xmlns:annotation="http://schemas.microsoft.com/ado/2009/02/edm/annotation" xmlns="http://schemas.microsoft.com/ado/2009/11/edm">
<Function Name="ParseDecimal" ReturnType="Edm.Decimal">
<Parameter Name="bytevalue" Type="Edm.Byte" />
<DefiningExpression>
cast(bytevalue as Edm.Decimal)
</DefiningExpression>
</Function>
C# 扩展方法,它是我项目根目录中的一个类 - 不在我的 EDMX 中
namespace BillYeagerDB
{
public partial class EdmxExtensionMethods : DbContext
{
[DbFunctionAttribute("BillYeagerDB", "ParseDecimal")]
public static Decimal ParseDecimal(byte bytevalue)
{
return Convert.ToDecimal(bytevalue);
}
}
}
更新了 Linq 查询 - 注意没有设计时编译错误并且项目编译成功
namespace BillYeagerDB
{
public class BillYeagerDB
{
public async Task<List<RestaurantList>> GetRestaurantListAsync()
{
try
{
using (BillYeagerEntities DbContext = new BillYeagerEntities())
{
DbContext.Database.Connection.Open();
var restaurants = await DbContext.Restaurants.GroupBy(g => g).Select(s =>
new RestaurantList()
{
Name = s.Key.Name,
City = s.Key.City,
Phone = s.Key.Phone,
AvgRating = s.Average(a => EdmxExtensionMethods.ConvertToDecimal(a.Rating.RatingValue)),
NbrOfPeopleRating = s.Distinct().Count(c => Convert.ToBoolean(c.RatingId)),
Id = s.Key.Id
}).ToListAsync();
return restaurants;
}
}
catch (Exception)
{
throw;
}
}
}
}
【问题讨论】:
-
您应该能够直接在查询中将字节转换为不同的类型,而无需使用自定义方法。也许不是直接加倍(我不确定),但如果不是,中间转换应该可以工作,也许是 byte -> int -> double。
-
您自己的解决方案也适用于我;您应该将其作为答案发布并接受,以便我们投票!
标签: c# .net asp.net-mvc entity-framework linq-to-entities