0

不能隐式转换类型“短?” 在LocalAmount = t.EmpNo处“做空” 。我使用过Convert.ToInt16(t.EmpNo),但随后该'join'子句将出错并成为"incorrect", "type inference failed..."

    public class AccountTransaction
    {
        public Int16 LocalAmount { get; set; }
        public String AccountNumber { get; set; }
    }

    public static IEnumerable<AccountTransaction> GetAllTransactions()
    {
        using (var context = new SQL_TA_SCOREBOARDEntities1())
        {
            return (from t in context.EmployeeAccesses
                    join acc in context.View_HCM
                         on t.EmpNo equals acc.EmpNo
                    select new AccountTransaction
                    {
                        LocalAmount = t.EmpNo,
                        AccountNumber = acc.EmailAddress

                    }).ToList();
        }
    }
4

2 回答 2

2

您的错误消息指出 t.EmpNo 是可为的Int 16。看到 short 后面的问号了吗?

'short?' to 'short'- 其中说:我无法将 a 转换为Int16?问号Int16定义的,该值可以是null.

所以如果你改变你的模型,你不需要解析任何东西,但你需要使用t.EmpNo.Value

public class AccountTransaction
{
    public Int16? LocalAmount { get; set; }
    public String AccountNumber { get; set; }
}
于 2014-03-07T06:19:11.620 回答
0

EmpNo 很可能永远不应该为空,并且您的 EmpNo 之一在数据库中可以为空,而另一个则不是。确保两者都不可为空。如果由于某种原因它们有时应该可以为 Null,那么它们在数据库中都必须可以为空。如果发生这种情况并且您在数据库中修复了它,您将不得不重新加载您的实体框架模型并再次尝试您的代码。您可能需要更改公共 Int16?本地金额 { 获取;放; } 返回公共 Int16 LocalAmount { get; 放; }。

本质上: Empno 必须在任何地方都不可为空或在任何地方都可以为空。在数据库和代码中。

于 2014-03-07T06:50:58.790 回答