[英]ASP.Net TO SQL Querying
我有以下問題:
業務問題:我在全球范圍內有超過500種ETF分為不同類型(世界區域,資本類型(小寫/大寫)),它們是SQL數據庫中的不同字段。
編碼問題:我能夠顯示ETF的整個列表,對其進行排序等。問題在於在同一網頁上創建5個不同的表(世界上5個區域)。 在SQL World中,這很簡單
Select * from ETF where RegionDS = 'Europe'
我今天的位置:我能夠查詢數據庫並檢索所有ETF,並在頁面上成功顯示它們,但無法以任何方式過濾它們。 這是一張桌子的M / V / C。 希望有人能為我拼湊而成。
模型
namespace SmartAdminMvc.Models
{
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.ComponentModel.DataAnnotations;
using System.ComponentModel.DataAnnotations.Schema;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
public partial class ETF
{
[Key]
public string Symbol { get; set; }
[Key]
public System.DateTime TodaysDate { get; set; }
public string SubSectorDS { get; set; }
public Nullable<int> RANK { get; set; }
public string RegionDS { get; set; }
視圖
@model IEnumerable<SmartAdminMvc.Models.ETF>
<table id="dt_basic" class="table table-striped table-bordered table-hover" width="100%">
<thead>
<tr>
<th> @Html.DisplayNameFor(model => model.RANK)</th>
<th> <a title=@Html.DisplayNameFor(model => model.Symbol)> @Html.DisplayNameFor(model => model.Symbol) </a> </th>
<th> <a title=@Html.DisplayNameFor(model => model.TodaysDate)>@Html.DisplayNameFor(model => model.TodaysDate) </a> </th>
<th> <a title=@Html.DisplayNameFor(model => model.SectorDS)>@Html.DisplayNameFor(model => model.SectorDS) </a> </th>
<th> <a title=@Html.DisplayNameFor(model => model.RegionDS)>@Html.DisplayNameFor(model => model.RegionDS) </a> </th>
</tr>
</thead>
<tbody>
@foreach (var item in Model.OrderBy(item => item.RANK).Take(50))
{
if (item.RegionDS == "Europe")
{
<tr>
<td align="right"> @Html.DisplayFor(modelItem => item.RANK) </td>
<td align="right"> @Html.DisplayFor(modelItem => item.Symbol) </td>
<td align="right"> @Html.DisplayFor(modelItem => item.TodaysDate) </td>
<td align="center"> @Html.DisplayFor(modelItem => item.SectorDS) </td>
<td align="center"> @Html.DisplayFor(modelItem => item.RegionDS) </td>
</tr>
}
}
</tbody>
</table>
控制器:
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Web.Mvc;
using SmartAdminMvc.Models;
namespace SmartAdminMvc.Controllers
{
public class ETFsController : Controller
{
private QlikEntities db = new QlikEntities();
// GET: vDailyPickSummaryTotals
public ActionResult ETFWorld()
{
return View(db.ETFs.ToList());
}
目前,您正在整個列表中排在前50位,然后嘗試按地區進行過濾。 但是,這只會返回那50條記錄中的匹配項(如果有)。 相反,請先執行過濾器:
<tbody>
@foreach (var item in Model.Where(w => w.RegionDS == "Europe").OrderBy(item => item.RANK).Take(50))
{
<tr>
<td align="right"> @Html.DisplayFor(modelItem => item.RANK) </td>
<td align="right"> @Html.DisplayFor(modelItem => item.Symbol) </td>
<td align="right"> @Html.DisplayFor(modelItem => item.TodaysDate) </td>
<td align="center"> @Html.DisplayFor(modelItem => item.SectorDS) </td>
<td align="center"> @Html.DisplayFor(modelItem => item.RegionDS) </td>
</tr>
}
</tbody>
編輯:
如果僅顯示記錄的子集,則可以創建如下的ViewModel:
public class MyViewModel
{
public IEnumerable<ETF> EuropeETFs {get; set;}
public IEnumerable<ETF> AsiaETFs {get; set;}
...
}
然后在您的控制器中:
MyViewModel vm = new MyViewModel();
vm.EuropeETFs = db.ETFs.Where(w => w.RegionDS == "Europe").OrderBy(item => item.RANK).Take(10);
....
將視圖的模型類型更改為MyViewModel,然后:
<tbody>
@foreach (var item in Model.EuropeETFs)
{
<tr>
<td align="right"> @Html.DisplayFor(modelItem => item.RANK) </td>
<td align="right"> @Html.DisplayFor(modelItem => item.Symbol) </td>
<td align="right"> @Html.DisplayFor(modelItem => item.TodaysDate) </td>
<td align="center"> @Html.DisplayFor(modelItem => item.SectorDS) </td>
<td align="center"> @Html.DisplayFor(modelItem => item.RegionDS) </td>
</tr>
}
</tbody>
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.