Casting a value in the extension method and the function in the edmx file is casting a value from a string to what we want.
That's fine as long as I would know what the a.Rating.RatingValue and c.RatingId values are ahead of time. They are not hardcoded, nor do I know what the values would be ahead of time.
UNfortunately, the first value is a Byte and the second value is an Int32.
So, in my extension methods, I have to set them up as follows. Note that Object.Parse(value) only works if the value is a string, so I can't do it like that.
With the below code, I'm still getting the same error message and is very frustrating. I don't know what else I need to do in order for it to convert successfully.
I have a requirement to get this in by tomorrow. If anyone could be of help, I would appreciate it.
Here is my query:
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;
}
Here are my extesnion methods. Again this class is outside the EDMX file on the root of the project:
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);
}
}
}
These are my functions inside the 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>
<Function Name="ParseBoolean" ReturnType="Edm.Boolean">
<Parameter Name="intvalue" Type="Edm.Int32" />
<DefiningExpression>
cast(intvalue as Edm.Boolean)
</DefiningExpression>
</Function>