1

When I'm trying to lookup a record by field name uuid I'm getting below error:

{"Unable to cast object of type 'System.Data.Entity.Infrastructure.DbQuery`1[SyncBank.Models.XeroBankAccount]' to type '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;
            }
        }

Error:

{"Unable to cast object of type 'System.Data.Entity.Infrastructure.DbQuery`1[SyncBank.Models.XeroBankAccount]' to type 'SyncBank.Models.XeroBankAccount'."}

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 Id { get; set; }

        [ForeignKey("XeroOrganisation")]
        [Column("organisation_id")]
        public int XeroOrganisationId { get; set; }

        public XeroOrganisation XeroOrganisation { get; set; }

        [Column("bank_id")]
        public int? BankId { get; set; }


        [Column("title")]
        [MinLength(1), MaxLength(128)]
        [Required]
        public String AccountTitle { get; set; }

        [Column("number")]
        [StringLength(50)]
        public String AccountNumber { get; set; }

        [Column("balance_statement")]
        public double? BalanceStatement { get; set; }

        [Column("balance_xero")]
        public double? BalanceXero { get; set; }

        [Column("last_statement_date")]
        public DateTime? LastStatementDate { get; set; }

        [Column("reconciled")]
        public BankAccountReconciled? Reconciled { get; set; }

        [Required]
        [Column("uuid")]
        [StringLength(256)]
        public string AccountUuid { get; set; }

        [Column("orders")]
        public int Orders { get; set; }

        public override int GetHashCode()
        {
            return Id.GetHashCode();
        }

        public override bool Equals(Object obj)
        {
            // TODO: Warning - this method won't work in the case the id fields are not set
            if (!(obj is XeroBankAccount))
            {
                return false;
            }
            XeroBankAccount other = (XeroBankAccount)obj;
            return Id == other.Id;
        }

        public override String ToString()
        {
            return "org.syncbank.entity.XeroBankAccount[ accountId=" + Id + " ]";
        }
    }
}

SQL xero_bank_account:

CREATE TABLE `xero_bank_account` (
  `id` int(11) NOT NULL,
  `organisation_id` int(11) NOT NULL,
  `bank_id` int(11) DEFAULT NULL,
  `title` varchar(128) NOT NULL,
  `number` varchar(50) DEFAULT NULL,
  `balance_statement` decimal(12,2) DEFAULT NULL,
  `balance_xero` decimal(12,2) DEFAULT NULL,
  `reconciled` enum('true','false') NOT NULL DEFAULT 'false',
  `last_statement_date` datetime DEFAULT NULL,
  `uuid` varchar(256) CHARACTER SET latin1 COLLATE latin1_bin NOT NULL,
  `orders` int(11) DEFAULT NULL
) ENGINE=InnoDB DEFAULT CHARSET=utf8;

--
-- Indexes for dumped tables
--

--
-- Indexes for table `xero_bank_account`
--
ALTER TABLE `xero_bank_account`
  ADD PRIMARY KEY (`id`),
  ADD UNIQUE KEY `account_uuid` (`uuid`),
  ADD KEY `account_title_caption` (`number`),
  ADD KEY `bank_id` (`bank_id`);
10
  • var result = dbContext.XeroBankAccounts.FirstOrDefault(x => x.AccountUuid == tempUuid); Commented Jan 25, 2019 at 11:45
  • Your result is an a collection not an object. Commented Jan 25, 2019 at 11:45
  • Sorry, initial error was solved, the issue when I use Linq I get {"Input string was not in a correct format."} Commented Jan 25, 2019 at 11:54
  • You can't just change your post to be a completely new question. Commented Jan 25, 2019 at 11:54
  • @DavidG My mistake sorry Commented Jan 25, 2019 at 11:55

1 Answer 1

5

You are trying to cast collection of XeroBankAccount to the single object of XeroBankAccount by using .Where() lambda expression so that's why you got an cast exception.

If you want to get your query must return single object of XeroBankAccount then, you can use any one of below

var result = (XeroBankAccount) dbContext.XeroBankAccounts.Where(x => x.AccountUuid == tempUuid).FirstOrDefault();

OR

var result = (XeroBankAccount) dbContext.XeroBankAccounts.FirstOrDefault(x => x.AccountUuid == tempUuid);

OR

var result = (XeroBankAccount) dbContext.XeroBankAccounts.Where(x => x.AccountUuid == tempUuid).Take(1).SingleOrDefault();
Sign up to request clarification or add additional context in comments.

6 Comments

I tried var result = (XeroBankAccount) dbContext.XeroBankAccounts.FirstOrDefault(x => x.AccountUuid == tempUuid and now I'm getting : 'dbContext.XeroBankAccounts.SingleOrDefault(x => x.AccountUuid == tempUuid)' threw an exception of type 'System.ArgumentException'
could you please provide exact exception message?
I was editing my question and I hit the submit by mistake, answers came in faster than I can edit my question! sorry about that
error is: "Method System.Data.Entity.Core.Objects.ObjectQuery1[T] MergeAs(System.Data.Entity.Core.Objects.MergeOption) contains generic parameters"
Here is : string tempUuid = uuid; var result = dbContext.XeroBankAccounts.SingleOrDefault(x => x.AccountUuid == tempUuid); return result;
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.