从SQL中的多个表填充ViewModel

时间:2016-07-27 01:37:06

标签: sql entity-framework linq model-view-controller

我尝试创建一个查询AdventureWorks2012数据库的SPA,并使用Jquery ui datepicker基于日期时间范围填充强类型表。

我遇到的问题是它返回所有相同的重复记录。

Duplicate Records

感谢您的帮助!

_Layout查看代码:

 <div>
    <h2>Pick a date range:</h2>
    <form action='@Url.Action("GetOrders","Home")' method="post">
    <label for="from">From</label>
    <input type="text" id="from" name="startDate">
    <label for="to">To</label>
    <input type="text" id="to" name="endDate">
    <input type='submit' value='Set Range' />
    </form>
 </div>

$( function() {
var dateFormat = "mm/dd/yy",
  from = $( "#from" )
    .datepicker({
      defaultDate: "+1w",
      changeMonth: true,
      numberOfMonths: 3
    })
    .on( "change", function() {
      to.datepicker( "option", "minDate", getDate( this ) );
    }),
  to = $( "#to" ).datepicker({
    defaultDate: "+1w",
    changeMonth: true,
    numberOfMonths: 3
  })
  .on( "change", function() {
    from.datepicker( "option", "maxDate", getDate( this ) );
  });

function getDate( element ) {
  var date;
  try {
    date = $.datepicker.parseDate( dateFormat, element.value );
  } catch( error ) {
    date = null;
  }

  return date;
}});

GetOrders.cshtml代码:

@model System.Collections.Generic.List<ApexAssignment.ViewModels.OrderViewModel>
@foreach (var item in Model)
{
    <tr>
        <td class="tg-yw4l">
            @item.Store
        </td>
        <td class="tg-yw4l">
            @item.Customer
        </td>
        <td class="tg-yw4l">
            @item.AccountNumber
        </td>
        <td class="tg-yw4l">
            @item.InvoiceNumber
        </td>
        <td class="tg-yw4l">
            @item.CustomerPO
        </td>
        <td class="tg-yw4l">
            @item.OrderDate
        </td>
        <td class="tg-yw4l">
            @item.DueDate
        </td>
        <td class="tg-yw4l">
            @item.InvoiceTotal
        </td>
        <td class="tg-yw4l">
            @item.ProductNumber
        </td>
        <td class="tg-yw4l">
            @item.Quantity
        </td>
        <td class="tg-yw4l">
            @item.UnitNet
        </td>
</tr>
}

控制器方法代码:

 [HttpPost]
    public ActionResult GetOrders(DateTime startDate, DateTime endDate)
    {
        var model = (from p in db.People
                     from pd in db.Products
                     from so in db.SalesOrderDetails
                     from sh in db.SalesOrderHeaders
                     where sh.OrderDate > startDate && sh.OrderDate < endDate
                     from s in db.Stores
                     from c in db.Customers
                     select new OrderViewModel()
                     {
                         AccountNumber = sh.AccountNumber,
                         Customer = p.FirstName + " " + p.LastName,
                         Store = s.Name,
                         ProductNumber = pd.ProductNumber,
                         DueDate = sh.DueDate,
                         CustomerPO = sh.PurchaseOrderNumber,
                         Quantity = so.OrderQty,
                         InvoiceNumber = sh.SalesOrderNumber,
                         UnitNet = so.UnitPrice,
                         InvoiceTotal = sh.TotalDue,
                         OrderDate = sh.OrderDate
                     });

        return View(model.ToList());
    }

1 个答案:

答案 0 :(得分:0)

var model = db.SalesOrderDetails
    .Where(e => e.SalesOrderHeader.OrderDate > startDate)
    .Where(e => e.SalesOrderHeader.OrderDate < endDate)
    .Join(db.Products, order => order.ProductID, 
        product => product.ProductID, (order, product) => new
        {
            Quantity = order.OrderQty,
            UnitNet = order.UnitPrice,
            DueDate = order.SalesOrderHeader.DueDate,
            CustomerPO = order.SalesOrderHeader.PurchaseOrderNumber,
            InvoiceNumber = order.SalesOrderHeader.SalesOrderNumber,
            InvoiceTotal = order.SalesOrderHeader.TotalDue,
            OrderDate = order.SalesOrderHeader.OrderDate,
            AccountNumber = order.SalesOrderHeader.AccountNumber,
            Store = order.SalesOrderHeader.Customer.Store.Name,
            Customer = order.SalesOrderHeader.Customer.Person.FirstName + " " +
                        order.SalesOrderHeader.Customer.Person.LastName,
            ProductNumber = product.ProductNumber
        }).ToList();