HDev007
HDev007

Reputation: 325

How to pass Stored Procedure with DateTime parameter in Entity Framework

public List<Data> GetData(string Date1, string Date2, string param3)
{
    var myData= new List<FindData_Result>();
    using (var context = new Config())
    {
        var queryResult = context.FindData(startDate, endDate, param3);
        final= (from a in queryResult select a).ToList();
    }
}

How do I pass date1 and end date2 as datetime parameter to my stored procedure?

I am using MVC web api with entity framework (DB first approach)

Upvotes: 0

Views: 3076

Answers (1)

Christos
Christos

Reputation: 53958

You should convert the string representations of your dates, start date and end date, to their DateTime equivalent. This can be done by using the Parse method of DateTime type, like below:

var startDate = DateTime.Parse(Date1);
var endDate = DateTime.Parse(Date2);
var queryResult = context.FindData(startDate, endDate, param3).ToList();

You could also use the ParseExact method, provided that your string have a specific format. For further info please have a look here.

Upvotes: 1

Related Questions