简体   繁体   中英

ASP.Net TO SQL Querying

I have the following problem:

Business Problem: I have over 500 ETF's around the world categorized into different types (Region of the world, Capital type (small cap/large cap) etc which are different fields in a SQL database.

Coding Problem: I am able to show the entire list of ETF's, sort them etc. The problem lies in creating 5 different tables (5 regions of the world) on the same web page. In the SQL World it would be a simple

Select * from ETF where RegionDS = 'Europe'

Where I am Today: I am able to query the database and retrieve all the ETF's and show them successfully on the page, but am not able to filter them in any way. Here is the M/V/C for just one table. Hopefully someone can piece it together for me.

MODEL

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; }

VIEW

@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>

CONTROLLER:

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());
        }

Currently you're taking the top 50 from the whole list, and then trying to filter out by region. But, this will only return the matches in those 50 records, if any. Instead, do the filter first:

      <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>

EDIT:

If you're only displaying a subset of the records you could create a ViewModel such as this:

public class MyViewModel
{
    public IEnumerable<ETF> EuropeETFs {get; set;}
    public IEnumerable<ETF> AsiaETFs {get; set;}
    ...
}

Then in your controller:

MyViewModel vm = new MyViewModel();
vm.EuropeETFs = db.ETFs.Where(w => w.RegionDS == "Europe").OrderBy(item => item.RANK).Take(10);
....

Change the model type of your View to MyViewModel, then:

      <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>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM