sag*_*y36 7 .net c# asp.net-mvc linq-to-entities entity-framework
我对LINQ to Entities比较新,但是使用LINQ to Sql很多.
我正在使用Visual Studio 2013与EntityFramework 6和MVC 5.
两者之间最大的区别是Linq2SQL能够在SELECT查询本身内执行转换,而LINQ2Entities不是宽容的,并且必须在执行LINQ查询之前进行正确的转换.因此,我正在错误:指定方法"System.Decimal ConvertToDecimal(字节)"的类型"BillYeagerDB.EdmxExtensionMethods"不能被翻译成LINQ实体储存表达.
在做了很多研究之后,特别是在使用这个问题的stackoveflow上,我发现了一个链接(LINQ to Entities不能识别方法'Double Parse(System.String)'方法,并且这个方法无法转换成商店表达式)工作.我确定作者给出了作品的例子,但他正在使用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;
}
}
}
}
Run Code Online (Sandbox Code Playgroud)
我需要对我的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>
Run Code Online (Sandbox Code Playgroud)
C#扩展方法,它是我项目根目录的一个类 - 不在我的EDMX中
namespace BillYeagerDB
{
public partial class EdmxExtensionMethods : DbContext
{
[DbFunctionAttribute("BillYeagerDB", "ParseDecimal")]
public static Decimal ParseDecimal(byte bytevalue)
{
return Convert.ToDecimal(bytevalue);
}
}
}
Run Code Online (Sandbox Code Playgroud)
更新了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;
}
}
}
}
Run Code Online (Sandbox Code Playgroud)
Dmy*_*nko -1
尝试将您的 select 方法重写为:
var restaurants = await DbContext.Restaurants.GroupBy(g => g).ToListAsync();
return restaurants.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
});
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
7439 次 |
| 最近记录: |