无法隐式转换类型 C#

本文关键字:类型 转换 | 更新日期: 2023-09-27 17:55:43

我有以下类:

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

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

无法隐式转换类型 C#

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

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

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

return result.ToList();

return (List<Callback>)result;

尝试更改

List<Callback>();
to
ToList();

什么是List<Callback>()的返回类型?我认为这是回报IList<Callback>.您必须明确将其转换为List<Callback>。你做一个必须明确完成的情况。