替换foreach以使循环成为可查询的

时间:2015-06-08 10:47:48

标签: c# performance linq asqueryable

我有一个返回报表对象的函数,但是目前我正在查看foreach,然后使用asQueryable方法。

我想在一个查询中执行此操作,而不必使用AsQueryable函数。

<html ng-app="ui.bootstrap.demo">

<head>
  <meta charset="UTF-8">
  <link rel="stylesheet" type="text/css" href="dummydisc/disc/styles/bootstrap.css" />
  <script type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/angularjs/1.3.15/angular.min.js"></script>
  <script type="text/javascript" src="dummydisc/disc/scripts/ui-bootstrap-tpls-0.13.0.min.js"></script>
  <style>
    img#dbIcon {
      margin: -1px 0px 0px;
      position: absolute;
      top: 2px;
      left: 1px;
    }

    label.btn.btn-primary {
      width: 65px;
    }
  </style>
  <title></title>
</head>

<body ng-controller="ButtonsCtrl">
  <pre>{{checkModel}}</pre>
  <div class="btn-group">
    <!--            Add DB names to label -->
    <label class="btn btn-primary" ng-model="checkModel.left" ng-click="toggleImage()" btn-checkbox><img ng-src="{{imageSwapUrl}}" />Left</label>
    <label class="btn btn-primary" ng-model="checkModel.middle" btn-checkbox><img id="dbIcon" src="images/database-5-16.ico" />Middle</label>
    <label class="btn btn-primary" ng-model="checkModel.right" btn-checkbox><img id="dbIcon" src="images/database-5-16.ico" />Right</label>
  </div>
  <?php // put your code here ?>
  <script>
    angular.module('ui.bootstrap.demo', ['ui.bootstrap']);
    angular.module('ui.bootstrap.demo').controller('ButtonsCtrl', function($scope) {
      $scope.imageSwapUrl = "images/database-5-16.ico";
      $scope.checkModel = {
        left: false,
        middle: true,
        right: false

      };
      $scope.toggleImage = function() {
        if ($scope.imageSwapUrl === 'images/database-5-16.ico') {
          $scope.imageSwapUrl = 'images/accept-database-16.ico';
        } else {
          $scope.imageSwapUrl = 'images/database-5-16.ico';
        }
      }
    });
  </script>
</body>
</html>

1 个答案:

答案 0 :(得分:2)

我想你想要这样的东西:

var query = from deal in context.Dealers
            where deal.ManufacturerId == manufacturerId
            from bodyshop in deal.Bodyshops1
            where bodyshop.Manufacturer2Bodyshop.Select(s => s.ManufacturerId).Contains(manufacturerId)
            let stat = bodyshop.Manufacturer2Bodyshop.FirstOrDefault(x => x.ManufacturerId == manufacturerId)
            orderby deal.Name
            select new DealerReport
            {
                Dealer = deal.Name,
                Bodyshop = bodyshop.Name,
                StatusShort = stat != null ? stat.ComplianceStatus : 0, // or some other default
            };

return query;