Linq查询返回错误

问题描述 投票:1回答:1

我正在将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查询。

 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>

这是数据库enter image description here

这是模型。

这是Screen Shot On Debugging模式.. enter image description here enter image description here

这是我运行applicationenter image description here时的结果

c# angularjs entity-framework linq wcf
1个回答
4
投票

尝试下面的代码,添加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
            }
        }
    }
}
© www.soinside.com 2019 - 2024. All rights reserved.