[英]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.