Reputation: 46282
I have the following LINQ Query. Note that DataValue
is a string
value. I am trying to convert it to a float and then get the average.
var Avg = (from fd in DataContext.FormData
select float.Parse(fd.DataValue)).Average();
I get the following error:
System.NotSupportedException: 'LINQ to Entities does not recognize
the method 'Single Parse(System.String)' method, and this method
cannot be translated into a store expression.'
Is there a good work around to this issue?
Upvotes: 1
Views: 1513
Reputation: 1279
Not every C# function is a mapped function to SQL. Depending on the ORM you are using (I'm assuming EntityFframework), you can find a list of mapped functions in the ORM documentation.
Mapped function basically mean a C# function that the ORM maps/converts to an SQL function and add it to the end result SQL statement that gets executed by the ORM.
Read the documentation to be safe.
If you still want to use Float.Parse
, then your only option is to materialize the query first then Parse, then Average. But that is not recommended.
Upvotes: 1
Reputation: 726919
You should be able to use Convert.ToSingle
instead of float.Parse
:
var Avg = DataContext.FormData
.Select(fd => DataContext.FormData(fd.DataValue))
.Average();
Upvotes: 2