Reputation: 24488
I need to combine 2 fields into 1 field. Name and Price. However, I am getting a LINQ error
LINQ to Entities does not recognize the method 'System.String ToString
Here is my linq statement.
var results = ctx.Plans.Select(p => new PlansView
{
PlanID = p.planID,
PlanActive = p.planActive,
PlanCost = p.planCost,
PlanName = p.planName,
PlanNameAndCost = p.planName + " " + Convert.ToString(p.planCost)
}).OrderBy(p => p.planName).ToList();
PlanNameAndCost = p.planName + " " + Convert.ToString(p.planCost) is causing the error. I understand that SQL does not have a ToString, but how can I get this to work?
Upvotes: 1
Views: 1486
Reputation: 726469
PlanNameAndCost = p.planName + " " + Convert.ToString(p.planCost)
is causing the error.
This is expected: the projection is executed on the RDBMS side; your SQL database has no idea what's ToString
. This member should be added when you are in memory, i.e. after AsEnumerable()
:
var results = ctx.Plans.Select(p => new PlansView
{
PlanID = p.planID,
PlanActive = p.planActive,
PlanCost = p.planCost,
PlanName = p.planName
}).OrderBy(p => p.planName)
.AsEnumerable().Select(p => new PlansView
{
PlanID = p.planID,
PlanActive = p.planActive,
PlanCost = p.planCost,
PlanName = p.planName,
PlanNameAndCost = p.planName + " " + Convert.ToString(p.planCost)
).ToList();
Upvotes: 2