Is it possible to change a field value from inside a LINQ select query for use in an ASP.NET MVC app?
For example, if a query returns a field with value "foo," I want it's output to be "bar" instead.
Essentially, I want LINQ to create this TSQL statement:
CREATE PROCEDURE GetSpecialCaseOfMyObject (
@param varchar(10)
) AS
SELECT
REPLACE( FieldA, 'foo', @param ) [FieldA]
FieldB,
FieldC
FROM
MyTable
WHERE
FieldA = @param
I've gotten this far, and it builds, but...
public IQueryable<MyObject> GetSpecialCaseOfMyObject (string myParam)
{
var result = from o in Context.MyObject
where o.FieldA = myParam
select new MyObject
{
FieldA = o.FieldA.Replace("foo", myParam),
FieldB = o.FieldB,
FieldC = o.FieldC
};
return result;
}
... when I try run a View to return data from it, I get a yellow screen of death:
Explicit construction of entity type 'Model.DataAccess.MyObject' in query is not allowed.
I tried using an anonymous type, but then I can't cast it back to MyObject.
Is what I want to do possible without jumping through all kinds of hoops?