我收到此错误:
无法投射类型的对象 'd__37'4 [System.Xml.Linq.XElement,BankOfBIT.Models.BankAccount,System.Int64,<> f__AnonymousType2'1 [System.Xml.Linq.XElement]]' 输入 'System.Collections.Generic.IEnumerable`1 [System.Xml.Linq.XElement]'。
我正在尝试执行这个lambda查询:
IEnumerable<XElement> transactionsWithMatchingAccountNumber
= (IEnumerable<XElement>) transactionswithValidAmount.Join(
db.BankAccounts,
transactions => long.Parse(transactions.Element("account_no").Value),
accounts => accounts.AccountNumber,
(transactions, accounts) => new { XElement = transactions });
我试图只获取与数据库中的帐号匹配的所有xml事务,以验证它们是否存在。
我有IEnumerable<XElement>
,其中包含以下内容:
<transaction>
<institution>34439</institution>
<account_no>10000</account_no>
<type>2</type>
<amount>50</amount>
<notes>withdrawal</notes>
</transaction>
我有2个EntityFramework MVC类与这些架构:
public class Transaction
{
[DatabaseGeneratedAttribute(DatabaseGeneratedOption.Identity)]
public int TransactionId { get; set; }
public long TransactionNumber { get; set; }
[ForeignKey("BankAccount")]
[Required]
public int BankAccountId { get; set; }
[ForeignKey("TransactionType")]
[Required]
public int TransactionTypeId { get; set; }
public double Deposit { get; set; }
public double Withdrawal { get; set; }
[Required]
public DateTime DateCreated { get; set; }
public string Notes { get; set; }
/// <summary>
/// create account with default next num
/// </summary>
public void SetNextTransactionNumber()
{
this.TransactionNumber = (long) StoredProcedures.NextNumber("NextTransactionNumbers");
}
public virtual BankAccount BankAccount { get; set; }
public virtual TransactionType TransactionType { get; set; }
}
public abstract class BankAccount
{
[DatabaseGeneratedAttribute(DatabaseGeneratedOption.Identity)]
public int BankAccountId { get; set; }
[Display(Name="Account\nNumber")]
public long AccountNumber { get; set; }
[Required]
[ForeignKey("Client")]
public int ClientId { get; set; }
[Required]
[ForeignKey("AccountState")]
public int AccountStateId { get; set; }
[Required]
[Display(Name = "Current\nBalance")]
[DisplayFormat(DataFormatString="{0:c2}")]
public double Balance { get; set; }
[Required]
[Display(Name = "Opening\nBalance")]
[DisplayFormat(DataFormatString = "{0:c2}")]
public double OpeningBalance { get; set; }
[Required]
[Display(Name = "Date\nCreated")]
[DisplayFormat(DataFormatString = "{0:d}")]
public DateTime DateCreated { get; set; }
[Display(Name = "Account\nNotes")]
public string Notes { get; set; }
/// <summary>
/// Returns the name of the account type
/// </summary>
[Display(Name = "Account\nType")]
public string Description
{
get
{
return GetType().Name.Split('_')[0].Replace("Account","");
}
}
/// <summary>
/// Stores the bank account state
/// </summary>
public virtual AccountState AccountState { get; set; }
/// <summary>
/// Stores the client associated with the bank account
/// </summary>
public virtual Client Client { get; set; }
/// <summary>
/// Increments the account number static counter
/// </summary>
public abstract void SetNextAccountNumber();
/// <summary>
/// Changes the state of the bank account
/// </summary>
public void ChangeState()
{
BankOfBITContext db = new BankOfBITContext();
AccountState lastState;
AccountState futureState;
do{
lastState = db.AccountStates.Find(AccountStateId);
lastState.StateChangeCheck(this);
futureState = db.AccountStates.Find(AccountStateId);
}while(lastState != futureState);
}
}
答案 0 :(得分:0)
尝试以下内容:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml;
using System.Xml.Linq;
namespace ConsoleApplication28
{
class Program
{
static void Main(string[] args)
{
string xml =
"<Root>" +
"<transaction>" +
"<institution>34439</institution>" +
"<account_no>10000</account_no>" +
"<type>2</type>" +
"<amount>50</amount>" +
"<notes>withdrawal</notes>" +
"</transaction>" +
"<transaction>" +
"<institution>34440</institution>" +
"<account_no>10001</account_no>" +
"<type>2</type>" +
"<amount>50</amount>" +
"<notes>withdrawal</notes>" +
"</transaction>" +
"</Root>";
XDocument doc = XDocument.Parse(xml);
List<XElement> tranactions = doc.Descendants("transaction").ToList();
var results = tranactions.Where(x => (int)x.Element("account_no") == 10000).Any();
}
}
}