c# - Linq-to-Entities SqlFunctions: How to Convert from int to double? or decimal? -
I am trying to sort some articles using Linq to Entities. I'm using System.Data.Objects.SqlClient.SqlFunctions. This is what I would like to do:
this.Queryable = this.Queryable. Orderbidescoding (v = & gt; v.UpVote + SqlFunctions.Log10 (SqlFunctions.DateDiff ("d", DateTime Today, v. Ready)));
However, this does not work, since Detoph is a int?
returns and log 10 or so takes a double?
or Decimal?
.
I could not find the convert function which it does in SqlFunctions. I tried to multiply it by multiplying 1.0 or casting it, but I get the following error:
System.Data.Entity.Core.EntityCommandExecutionException: When reading from the store reader's data reader Error occurred. See internal exceptions for details ---> System.Data.SqlClient.SqlException: An invalid floating point operation occurred.
How do I change from int? Double
to
?
or Decimal?
Using SqlFunctions? Based on the reaction of Ocelot20, I switched around the arguments and added the created day one day so that it is negative or 0 in input Do not produce, which was caused by error. It was also necessary to add an up vote to repeat the working version that looks like this:
var date = DateTime.Today.AddDays (1); this. Connectionable = This. Contextual. Order ByDesking (v = & gt; (double) v.UpVote - SqlFunctions.Log10 (Math.Ebs (Double) SqlFunctions.DateDiff ("D", Date, V. Ready));
The exception is suggesting that the problem is related to the operation instead of the problem, with changing the types involved. Using LOG10
in SQL will throw off this exception when a negative number will be passed (try: SELECT LOG10 (-1)
). If SqlFunctions .DateDiff
gives a negative number (which can be based on created date
date), your query will fail.
I'm guessing that just like this To work with the proper castings of DateTime.Today
and v.Created
need to switch:
this.Queryable < V> (v = & gt; v.UpVote + SqlFunctions.Log10 ((double?) SqlFunctions.DateDiff ("d", v. Ready, DateTime.));
v.Created
is to include future dates or in There is some negative time difference that you can use to make Math.Abs
so that it is always positive:
this. Connectionable Select (V => v.UpVote + SqlFunctions.Log10 ((double?) Math.Abs (SqlFunctions.DateDiff ("d", DateTime.Today, v.Created));
Edit - In addition to failing with negative numbers, LOG10
0 will be unsuccessful if provided. Take out 0, or modify it Ignore the form:
Enable v. Of this dayDiff = SqlFunctions.DateDiff ("d", v. Created, datetime today) Vote = vide = day def == 0? 0: Select SqlFunctions.Log10 ((double?) DayDiff) v.UpVote + voteWyight
Select this exact query Has not tested, but the impression is that this number is less or equal than it is necessary to handle in any way.
Comments
Post a Comment