无法隐式转换类型C#

时间:2014-04-01 13:31:27

标签: c#

我有以下课程:

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;
    }
}

在返回时,我收到以下错误:

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?)

我现在已经看了半个小时了,根本看不出我错过了什么?

3 个答案:

答案 0 :(得分:2)

声明方法时,您是否尝试过IList<Callback>返回类型?

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

如果您在调用此方法时需要准确获取List,您还可以尝试返回:

return result.ToList();

return (List<Callback>)result;

答案 1 :(得分:0)

尝试更改

List<Callback>();

to

ToList();

答案 2 :(得分:0)

List<Callback>()的返回类型是什么?我认为这是回归IList<Callback>。你必须明确地把它投到List<Callback>。你做了必须明确做的案例。