c# - 无法转换为 LINQ to Entities 存储表达式

标签 c# .net asp.net-mvc entity-framework linq-to-entities

我对 LINQ-to-Entities 比较陌生,但经常使用 LINQ-to-SQL。

我正在将 Visual Studio 2013 与 Entity Framework 6 和 MVC 5 结合使用。

两者最大的区别是Linq2SQL有能力在SELECT内部进行转换查询本身,而 LINQ2Entities 不那么宽容,并且在执行 LINQ 查询之前必须进行正确的转换。因此,我收到错误:

The specified method 'System.Decimal ConvertToDecimal(Byte)' on the type 'BillYeagerDB.EdmxExtensionMethods' cannot be translated into a LINQ to Entities store expression.

在做了很多研究之后,特别是关于这个问题的 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 .

我也确信它对我有用,但我认为我只是错误地设计了扩展方法(这给了我上述错误)。请注意,此特定问题与 AvgRating 有关LINQ 查询中的变量。一旦我可以让它工作,我就可以对任何其他转换进行相同类型的修复。注意 AvgRating被定义为类型 Decimala.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;
            }
        }
    }
}

最佳答案

我知道我来晚了一点,但对于任何使用 Google 搜索的人来说:

我遇到了这个问题,结果发现 DbFunctionAttribute 所在的类必须与 edmx 架构具有相同的命名空间。

所以在这种情况下,要么将 edmx 架构命名空间更改为 BillYeagerDB,
或将 EdmxExtensionMethods 命名空间更改为 BillYeagerModel

关于c# - 无法转换为 LINQ to Entities 存储表达式,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20182823/

相关文章:

c# - 将 -1 转换为 uint32 的目的?

c# - PInvoke 致命执行引擎错误

c# - 如何注销匿名处理程序?

C# linq filter 加 concat

c# - 如何在 ASP.NET MVC4 上打开 customErrors 模式?

c# - 在文件夹中保存具有唯一名称的上传文件

c# - QNetworkAccessManager->在 DLL 中调用时卡住

c# - 如何将所有列作为属性的 DataTable 导出到 Xml?

asp.net-mvc - Entity Framework : updating one-many relationships for an entity's child collection

javascript - ASP.NET MVC : Action Method and automatically binding to an array of values