小编Mas*_*ase的帖子

无法转换类型为“System.Data.Entity.Infrastruct.DbQuery”的对象

当我尝试按字段名称查找记录时,uuid出现以下错误:

{“无法将类型为“System.Data.Entity.Infrastruct.DbQuery`1[SyncBank.Models.XeroBankAccount]”的对象转换为类型为“SyncBank.Models.XeroBankAccount”

public XeroBankAccount FindAccountByUuid(String uuid)
        {
            try
            {
                using (SyncBankDbContext dbContext = new SyncBankDbContext())
                {
                    string tempUuid = uuid;
                    var result = (XeroBankAccount) dbContext.XeroBankAccounts.Where(x => x.AccountUuid == tempUuid);
                    return result;
                }
            }
            catch (Exception e)
            {
                string errorMessage = e.Message;
                return null;
            }
        }
Run Code Online (Sandbox Code Playgroud)

错误:

{"Unable to cast object of type 'System.Data.Entity.Infrastructure.DbQuery`1[SyncBank.Models.XeroBankAccount]' to type 'SyncBank.Models.XeroBankAccount'."}
Run Code Online (Sandbox Code Playgroud)

XeroBankAccount.cs:

using System;
using System.ComponentModel.DataAnnotations.Schema;
using System.ComponentModel.DataAnnotations;
using SyncBank.Xero;

namespace SyncBank.Models
{
    [Serializable]
    [Table("xero_bank_account")]
    public class XeroBankAccount
    {

        [Key]
        [Column("id")]
        public int …
Run Code Online (Sandbox Code Playgroud)

c# linq entity-framework

1
推荐指数
1
解决办法
3486
查看次数

标签 统计

c# ×1

entity-framework ×1

linq ×1