不能被翻译成LINQ到实体店EX pression [英] Cannot be translated into a LINQ to Entities store expression

查看:225
本文介绍了不能被翻译成LINQ到实体店EX pression的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是比较新的LINQ到实体,而是使用LINQ到SQL很多。

我在使用Visual Studio 2013的EntityFramework 6和MVC 5。

这两者之间最大的区别在于,LINQ2SQL有SELECT查询自身内部进行转换,而LINQ2Entities是不能容忍的,必须执行LINQ查询之前已经制定权转换的能力。因此,我得到的错误:指定的方法System.Decimal ConvertToDecimal(字节)'的类型'BillYeagerDB.EdmxExtensionMethods'不能被翻译成LINQ到实体店EX pression

在做大量的研究,尤其是在stackoveflow这个问题之后,我发现了一个链接(<一href="http://stackoverflow.com/questions/5971521/linq-to-entities-does-not-recognize-the-method-double-parsesystem-string-met">LINQ以实体不能识别方法双解析(System.String)的方法,而这种方法不能被翻译成店前pression )有希望会工作。我敢肯定,例如笔者给出的作品,但他正与ObjectContext中,我用的DbContext工作。

我也相信它会为我工作,但我觉得我只是在设计的扩展方法不正确(这给了我上面的错误)。请注意,这个特定的问题是,在LINQ查询中的 AvgRating 的变量。有一次,我能得到这个工作,我可以做同样类型的固定任何其他的转换。注意的 AvgRating 的被定义为类型十进制和 a.Rating.RatingValue 的被定义为类型字节。

如果有人能理顺我出去,我会大大AP preciate吧。

下面是我的code。我试图用下面的查询,我知道是不行的(如前所述),因为转换的问题。

原LINQ查询:

 命名空间BillYeagerDB
{
    公共类BillYeagerDB
    {
        公共异步任务&LT;列表&LT; RestaurantList&GT;&GT; GetRestaurantListAsync()
        {
            尝试
            {
                使用(BillYeagerEntities的DbContext =新BillYeagerEntities())
                {
                    DbContext.Database.Connection.Open();

                    VAR餐馆=等待DbContext.Restaurants.GroupBy(G =&GT; G)。选择(S =&GT;
                        新RestaurantList()
                        {
                            名称= s.Key.Name,
                            市= s.Key.City,
                            手机= s.Key.Phone,
                            AvgRating = s.Average(一个=&GT; Convert.ToDecimal(a.Rating.RatingValue)),
                            NbrOfPeopleRating = s.Distinct()计数(C =&GT; Convert.ToBoolean(c.RatingId)),
                            n = s.Key.Id
                        })ToListAsync()。

                    返回餐厅;
                }
            }
            赶上(例外)
            {
                扔;
            }
        }
    }
}
 

更新,我需要做我的EDMX文件

 &LT; EDMX:ConceptualModels&GT;
      &LT;模式命名空间=BillYeagerModel别名=自我的注释:UseStrongSpatialTypes =假的xmlns:诠释=htt​​p://schemas.microsoft.com/ado/2009/02/edm/annotation的xmlns =HTTP: //schemas.microsoft.com/ado/2009/11/edm">
        &lt;作用NAME =ParseDecimal的返回类型=Edm.Decimal&GT;
            &LT;参数名称=的byteValueTYPE =Edm.Byte/&GT;
            &LT; D​​efiningEx pression&GT;
                投(为的byteValue Edm.Decimal)
            &LT; / DefiningEx pression&GT;
        &LT; /功能&GT;
 

C#的扩展方法,它是一类对我的项目的根 - 而不是在我的EDMX

 使用系统;
使用System.Collections.Generic;
使用System.Linq的;
使用System.Text;
使用System.Threading.Tasks;
使用System.Data.Entity的;

命名空间BillYeagerDB
{
    公共部分类EdmxExtensionMethods:的DbContext
    {
        [DbFunctionAttribute(BillYeagerDB,ParseDecimal)]
        公共静态小数ParseDecimal(字节的byteValue)
        {
            返回Convert.ToDecimal(的byteValue);
        }
    }
}
 

更新LINQ查询 - 注意,没有设计时编译错误和项目成功编译

 命名空间BillYeagerDB
{
    公共类BillYeagerDB
    {
        公共异步任务&LT;列表&LT; RestaurantList&GT;&GT; GetRestaurantListAsync()
        {
            尝试
            {
                使用(BillYeagerEntities的DbContext =新BillYeagerEntities())
                {
                    DbContext.Database.Connection.Open();

                    VAR餐馆=等待DbContext.Restaurants.GroupBy(G =&GT; G)。选择(S =&GT;
                        新RestaurantList()
                        {
                            名称= s.Key.Name,
                            市= s.Key.City,
                            手机= s.Key.Phone,
                            AvgRating = s.Average(一个=&GT; EdmxExtensionMethods.ConvertToDecimal(a.Rating.RatingValue)),
                            NbrOfPeopleRating = s.Distinct()计数(C =&GT; Convert.ToBoolean(c.RatingId)),
                            n = s.Key.Id
                        })ToListAsync()。

                    返回餐厅;
                }
            }
            赶上(例外)
            {
                扔;
            }
        }
    }
}
 

解决方案

尝试重写你的选择的方法如下:

  VAR餐馆=等待DbContext.Restaurants.GroupBy(G =&GT; G).ToListAsync();
返回restaurants.Select(S =&GT;
  新RestaurantList()
  {
    名称= s.Key.Name,
    市= s.Key.City,
    手机= s.Key.Phone,
    AvgRating = s.Average(一个=&GT; Convert.ToDecimal(a.Rating.RatingValue)),
    NbrOfPeopleRating = s.Distinct()计数(C =&GT; Convert.ToBoolean(c.RatingId)),
    n = s.Key.Id
  });
 

I am relatively new to LINQ to Entities, but use LINQ to Sql a lot.

I am using Visual Studio 2013 with EntityFramework 6 and MVC 5.

The biggest difference between the two is that Linq2SQL has the ability to perform conversions inside the SELECT query itself whereas LINQ2Entities is not as forgiving and must have the right conversion in place before executing the LINQ query. Therefore, I am getting the error: The specified method 'System.Decimal ConvertToDecimal(Byte)' on the type 'BillYeagerDB.EdmxExtensionMethods' cannot be translated into a LINQ to Entities store expression.

After doing much research, especially on stackoveflow with this question, I discovered a link (LINQ to Entities does not recognize the method 'Double Parse(System.String)' method, and this method cannot be translated into a store expression) that hopefully would work. I'm sure the example the author gives works, but he was working with the ObjectContext and I am working with the DbContext.

I'm also sure it will work for me, but I think I'm just designing the extension method incorrectly (which gives me the above error). Note that this specific issue is with the AvgRating variable in the Linq query. Once I can get this to work, I can do the same type of fixing for any other conversions. Note that AvgRating is defined as type Decimal and a.Rating.RatingValue is defined as type Byte.

If somebody can straighten me out on this, I would greatly appreciate it.

Here is my code. I'm trying to use the following query, which I know won't work (as mentioned before) because of the conversion issue.

Original LINQ Query:

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;
            }
        }
    }
}

Update I needed to do to my EDMX file

<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# extension method which is a class on the root of my project - not inside my EDMX

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Data.Entity;

namespace BillYeagerDB
{
    public partial class EdmxExtensionMethods : DbContext
    {
        [DbFunctionAttribute("BillYeagerDB", "ParseDecimal")]
        public static Decimal ParseDecimal(byte bytevalue)
        {
            return Convert.ToDecimal(bytevalue);
        }
    }
}

Updated Linq query - note no design time compile errors and project compiles successfully

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;
            }
        }
    }
}

解决方案

try to rewrite your select method to this:

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
  });

这篇关于不能被翻译成LINQ到实体店EX pression的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆