gpt4 book ai didi

c# - Linq 查询返回错误

转载 作者:太空宇宙 更新时间:2023-11-03 20:58:38 25 4
gpt4 key购买 nike

我正在将 wcf rest 服务消耗到 angular js 应用程序中。我将三个表记录连接到一个记录中并将其显示在 Web 应用程序中。我希望当我单击带有帐号的按钮时,它应该返回用户帐户信息,如帐号、入金、出金、日期和账户余额等。但是我在 google chrome 网络选项卡中出现以下错误..

服务器在处理请求时遇到错误。异常消息是“类型‘HalifaxWCFProject.Transcation.AccountTransaction’出现在单个 LINQ to Entities 查询中的两个结构上不兼容的初始化中。一个类型可以在同一个查询的两个地方初始化,但前提是在两个地方都设置了相同的属性并且这些属性以相同的顺序设置。有关详细信息,请参阅服务器日志。异常堆栈跟踪是:

这是类。

   public class AccountTransaction
{
public int? Account_Number { get; set; }

public decimal? Deposit { get; set; }

public decimal? Withdrawal { get; set; }

public decimal? Account_Balance { get; set; }

public string Date { get; set; }

}

这是 Linq 查询。

 public string TranscationDetails(string Account_Number)
{
var accountNumber = int.Parse(Account_Number);//It could be better to use TryParse
using (HalifaxDatabaseEntities context = new HalifaxDatabaseEntities())
{
var inOut = context.Current_Account_Deposit.Where(x => x.Account_Number == accountNumber).Select(w => new AccountTransaction
{
Account_Number = w.Account_Number,
Deposit = (decimal?)null,
Withdrawal = (decimal?)w.Amount,
Date = w.Date
}).Concat(context.Current_Account_Withdraw.Where(x => x.Account_Number == accountNumber).Select(d => new AccountTransaction
{
Account_Number = d.Account_Number,
Deposit = (decimal?)d.Amount,
Withdrawal = (decimal?)null,
Date = d.Date
})).OrderBy(r => r.Date)
.Concat(context.Current_Account_Details.Where(x => x.Account_Number == accountNumber).Select(e => new AccountTransaction
{
Account_Number = e.Account_Number,
Account_Balance = (decimal?)e.Account_Balance
}));
var js = new System.Web.Script.Serialization.JavaScriptSerializer();
return js.Serialize(inOut); // return JSON string
}
}
}
}

这是我的 Angular js 代码。

@{
Layout = null;
}

<!DOCTYPE html>

<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title></title>
</head>
<body>
<script type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/angularjs/1.3.9/angular.min.js"></script>
<script type="text/javascript">
var app = angular.module('MyApp', [])
app.controller('MyController', function ($scope, $http, $window) {
$scope.IsVisible = false;
$scope.Search = function () {
var post = $http({
method: "GET",
url: "http://localhost:52098/HalifaxIISService.svc/TranscationDetails/" + encodeURIComponent($scope.Account_Number),
dataType: 'json',
headers: {
'Accept': 'application/json, text/javascript, */*; q=0.01',
'Content-Type': 'application/json; charset=utf-8'
}
});

post.then(function (response) { // .success(function(data => .then(function(response
var data = response.data; // extract data from resposne
$scope.Customers = JSON.parse(data); // eval(data.d) => JSON.parse(data)
$scope.IsVisible = true;
}, function (err) {
$window.alert(err);
});
}
});
</script>
<div ng-app="MyApp" ng-controller="MyController">
Account Number:
<input type="text" ng-model="Account_Number" />
<input type="button" value="Submit" ng-click="Search()" />
<hr />
<table style="border: solid 2px Green; padding: 5px;" ng-show="IsVisible">
@*<table cellpadding="0" cellspacing="0">*@
<tr style="height: 30px; background-color: skyblue; color: maroon;">
<th></th>
<th> Account Number</th>
<th> Money In</th>
<th>Date</th>
<th> Money Out</th>
<th>Date</th>
<th>Account Balance</th>

<th></th>
<th></th>

</tr>
<tbody ng-repeat="m in Customers">
<tr style="height: 30px; background-color: skyblue; color: maroon;">
<td></td>
<td><span>{{m.Account_Number}}</span></td>
<td><span>{{m.Deposit| currency:"£"}}</span></td>
<td><span>{{m.Date}}</span></td>

<td><span>{{m.Withdrawal | currency:"£"}}</span></td>
<td><span>{{m.Date}}</span></td>
<td><span>{{m.Account_Balance| currency:"£"}}</span></td>

</tr>

</tbody>
</table>
</div>
</body>
</html>

这是数据库enter image description here

这是模型。

这是 Debug模式下的屏幕截图.. enter image description here enter image description here

这是我运行应用程序 enter image description here 时的结果

最佳答案

尝试下面的代码,添加 AccountTransaction 的所有属性添加使用 Union 而不是 Concat

Union 将返回不同的值,但此处您的比较是引用您的项目,因此所有项目都将被视为不同。 Concat 等同于Union All

 public string TranscationDetails(string Account_Number)
{
var accountNumber = int.Parse(Account_Number);//It could be better to use TryParse
using (HalifaxDatabaseEntities context = new HalifaxDatabaseEntities())
{
var inOut = context.Current_Account_Deposit.Where(x => x.Account_Number == accountNumber).Select(w => new AccountTransaction
{
Account_Number = w.Account_Number,
Account_Balance = (decimal?)0M,
Deposit = (decimal?)null,
Withdrawal = (decimal?)w.Amount,
Date = w.Date
}).Union(context.Current_Account_Withdraw.Where(x => x.Account_Number == accountNumber).Select(d => new AccountTransaction
{
Account_Number = d.Account_Number,
Account_Balance = (decimal?)0M,
Deposit = (decimal?)d.Amount,
Withdrawal = (decimal?)null,
Date = d.Date
})).OrderBy(r => r.Date)
.Union(context.Current_Account_Details.Where(x => x.Account_Number == accountNumber).Select(e => new AccountTransaction
{
Account_Number = e.Account_Number,
Account_Balance = (decimal?)e.Account_Balance,
Deposit = (decimal?)0M,
Withdrawal = (decimal?)0M,
Date = DateTime.Now
}));
var js = new System.Web.Script.Serialization.JavaScriptSerializer();
return js.Serialize(inOut); // return JSON string
}
}
}
}

关于c# - Linq 查询返回错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47935660/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com