【问题标题】:The Linq Query Returns ErrorsLinq 查询返回错误
【发布时间】:2017-12-22 03:58:28
【问题描述】:

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

这里是数据库

这是模型。

这是调试模式的屏幕截图..

这是我运行应用程序时的结果

【问题讨论】:

  • 你试过用Union代替Concat吗?
  • 你过去 2 天的所有问题都是一样的。你不明白问题的陈述是错误的。说“我将三个表记录合并为一条记录 ...它应该返回用户帐户信息,例如帐号、入金、出金、日期和帐户余额”这是非常错误的。因为您可能使用此帐户在不同的日子进行大量操作,但您想要一个带日期的记录。您应该从很多操作中选择哪个日期?你懂吗?此外,ConcatUnion 正在连接 ROWS,而不是 Columns!!!
  • 或者您可能想说“将三个表记录连接到一个 VIEW(表)中”。你必须明白一件事:“单一记录”和“单一视图”是完全不同的东西

标签: c# angularjs entity-framework linq wcf


【解决方案1】:

试试下面的代码,添加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
            }
        }
    }
}

【讨论】:

  • 非常感谢。出色的帮助@programtreasures
  • 0M 是你所说的(decimal)0 因为M 是表示十进制的后缀
猜你喜欢
  • 1970-01-01
  • 2021-02-04
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多