I'm working for the first time with LINQ to Entities.
My data store has a string field that depending on certain other fields is often an integer. I cannot change the structure of this database.
The table in question in the EF is masterdatas.
I am attempting to create the sum for these records in the varvalue field based on a grouping based on the varname field as such:
var varsum = from md in context.masterdatas
where md.imparcid == SelectedDataSet.imparcid && md.varname == t.VarName
group md by md.varname into g
select new { varname = g.Key, SummedAmt = g.Sum(md => Convert.ToDouble(md.varvalue)) };
I get an error LINQ to Entities does not recognize method Double ToDouble(system string).
Based on this SO question, I added a DefiningExpression to my EDMX model, and defined the function in a partial class of my datamodel and used this code:
var varsum = from md in context.masterdatas
where md.imparcid == SelectedDataSet.imparcid && md.varname == t.VarName
group md by md.varname into g
select new { varname = g.Key, SummedAmt = g.Sum(md => context.ParseDouble(md.varvalue)) };
Now I'm getting an error that the ParseDouble function cannot be translated into a LINQ to entities store expression.
All I'm really trying to do is to get this SQL statement to run:
SELECT [varname]
,[vartype]
,Sum(CASE WHEN Isnumeric([varvalue])=1 THEN cast([varvalue] as float) Else 0 END)
FROM [masterdata]
Group by varname, imparcid, vartype
Having imparcid=25 AND varname = 'S2CMT8B'
I'm at a loss about the best way to get this done.