无法从GroupBy LINQ查询中转换为LINQ to Entities存储表达式

时间:2013-11-25 19:45:50

标签: c# linq entity-framework linq-to-entities

在扩展方法中转换值,edmx文件中的函数将字符串中的值转换为我们想要的值。

只要我知道 a.Rating.RatingValue c.RatingId 的值是提前的,那就没关系。它们没有硬编码,我也不知道它们的价值会是什么。

不幸的是,第一个值是字节,第二个值是Int32。

所以,在我的扩展方法中,我必须按如下方式设置它们。请注意, Object.Parse(value)仅在值为字符串时才有效,所以我不能这样做。

使用下面的代码,我仍然收到相同的错误消息,非常令人沮丧。我不知道还需要做什么才能让它成功转换。

我明天要求明白。如果有人可以提供帮助,我将不胜感激。

这是我的问题:

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.ParseDecimal(a.Rating.RatingValue)),
                            NbrOfPeopleRating = s.Distinct().Count(c => EdmxExtensionMethods.ParseBoolean(c.RatingId)),
                            Id = s.Key.Id
                        }).ToListAsync();

                    return restaurants;
                }

以下是我的extesnion方法。同样,这个类位于项目根目录下的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(Int16 int16value)
        {
            return Convert.ToDecimal(int16value);
        }

        [DbFunctionAttribute("BillYeagerDB", "ParseBoolean")]
        public static bool ParseBoolean(Int32 intvalue)
        {
            return Convert.ToBoolean(intvalue);
        }
    }
}

这些是我在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>
        <Function Name="ParseBoolean" ReturnType="Edm.Boolean">
          <Parameter Name="intvalue" Type="Edm.Int32" />
          <DefiningExpression>
            cast(intvalue as Edm.Boolean)
          </DefiningExpression>
        </Function>

0 个答案:

没有答案
相关问题