2

I am using ASP.NET entity framework and I am trying to create an SqlQuery but I get this error:

The specified cast from a materialized 'System.Int64' type to the 'System.Int32' type is not valid.

My problem is I don't know which column is giving me trouble and how to fix it. Here is my code. I don't think its my casts that I am doing because those are strings.

return View(db.Data.SqlQuery("SELECT ROW_NUMBER() OVER(ORDER BY slotOrder ASC) AS id, 
                              concat(a.dateSlot, ' - ', a.timeSlot) as dateSlot, 
                              concat(a.dateSlot, '-', a.timeSlot) as timeSlot, 
                              COUNT(*) as slotOrder 
                              FROM Appointments a 
                              LEFT OUTER JOIN dateTimeSlots b 
                              ON a.dateSlot = b.dateSlot AND a.timeSlot = b.timeSlot 
                              GROUP BY a.dateSlot, a.timeSlot, b.slotOrder 
                              ORDER BY b.slotOrder").ToList());

Here is my class:

public class dateTimeSlots
{
    public int id { get; set; }
    [DisplayName("Date")]
    public string dateSlot { get; set; }
    [DisplayName("Time")]
    public string timeSlot { get; set; }
    [DisplayName("Order")]
    public int slotOrder { get; set; }
}
2
  • What is the model class yo are storing it into? Please add it too. Either id or slotOrder are longs and not ints. Probably id Commented Sep 5, 2017 at 17:45
  • Added Class to my question. Commented Sep 5, 2017 at 17:48

2 Answers 2

10

Your query above returns an Int64 (long) and you are storing it as an Int32 (int). Find out which of the numeric fields it is and adjust model.

My guess is that the id is long as it is ROW_NUMBER().

Sign up to request clarification or add additional context in comments.

Comments

2

ROW_NUMBER() returns a bigint (MSDN), which translates as Int64 or long in C#.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.