Marc Rasmussen
Marc Rasmussen

Reputation: 20565

Cannot implicitly convert type C#

i have the following class:

public class CallbackModel : SharedModel
{
    public CallbackModel() : base()
    {

    }

    public List<Callback> getData(DateTime start, DateTime end)
    {

        StringBuilder query = new StringBuilder();
        query.Append("SELECT   LAST_UPD AS PERIOD, ");
        query.Append("COUNT(CASE WHEN STATUS ='Færdig' THEN 1 END) as completed, ");
        query.Append("COUNT(CASE WHEN SOLVED_SECONDS /60 /60 <= 2 THEN 1 END) as completed_within_2hours ");
        query.Append("FROM     KS_DRIFT.NYK_SIEBEL_CALLBACK_AGENT_H_V ");
        query.Append("WHERE    LAST_UPD BETWEEN '" + start.ToString("yyyy-MM-dd") + "' AND '" + end.ToString("yyyy-MM-dd") + "' ");
        query.Append("AND      STATUS ='Færdig' ");
        query.Append("GROUP BY LAST_UPD ORDER BY LAST_UPD ASC");
        var session = sessionFactory.OpenSession();
        var result = session.CreateSQLQuery(query.ToString())
            .AddEntity(typeof(Callback))
            .List<Callback>();

        return result;
    }
}

At the return i get the following error:

Error   19  Cannot implicitly convert type 'System.Collections.Generic.IList<Henvendelser.Objects.Callback>' to 'System.Collections.Generic.List<Henvendelser.Objects.Callback>'. An explicit conversion exists (are you missing a cast?)

I have been looking at this for half an hour now and simply cannot see what i am missing?

Upvotes: 0

Views: 422

Answers (3)

prvit
prvit

Reputation: 966

Did you try IList<Callback> return type when declare a method ?

public IList<Callback> getData(DateTime start, DateTime end)
{
     // your code here
}

If you need to get exactly the List when call this method, you can also try to return:

return result.ToList();

or

return (List<Callback>)result;

Upvotes: 2

Hamlet Hakobyan
Hamlet Hakobyan

Reputation: 33391

What is return type of List<Callback>()? I think it is return IList<Callback>. YOu must explicitly cast it to List<Callback>. You make down case which must be done explicitly.

Upvotes: 0

testydonkey
testydonkey

Reputation: 149

Try changing

List<Callback>();

to

ToList();

Upvotes: 0

Related Questions