我正在将wcf rest服务消耗到angular js应用程序中。我将三个表记录合并为一个记录,并将其显示在Web应用程序中。我想当我单击带有帐号的按钮时,它应该返回用户帐号信息,例如帐号,入金,出金,日期和帐户余额等。但是我在谷歌浏览器网络标签中出现以下错误..
服务器在处理请求时遇到错误。异常消息为“类型'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 Query。
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
}
}
}
}
这是我的角度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>
这是数据库
这是模型。
这是调试模式下的屏幕截图。
这是我运行应用程序的结果
最佳答案
尝试下面的代码,添加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
}
}
}
}