在MVC中如何选择不同的日期?

huangapple go评论48阅读模式
英文:

How to select distinct DATE in MVC?

问题

以下是翻译好的部分:

我想选择不同的日期,但不起作用且重复

这是我尝试的SQL Server中的SELECT语句,它在那里有效:

SELECT DISTINCT date
FROM STATISTICS_DAILY
WHERE hospital_no = 1

这是我得到的输出:

2023-06-06 00:00:00.000
2023-06-07 00:00:00.000
2023-06-08 00:00:00.000
2023-06-08 15:21:46.000
2023-06-09 00:00:00.000
2023-06-10 00:00:00.000

但在ASP.NET MVC中,如何获得相同的结果,这是`Index`控制器:

public ActionResult Index()
{
    int HospitalId = Convert.ToInt32(Session["HospitalID"]);

    var sTATISTICS_DAILY = db.STATISTICS_DAILY
                             .Where(x => x.hospital_no == HospitalId)
                             .OrderBy(d => d.DATE)
                             .Distinct()
                             .ToList();
    return View(sTATISTICS_DAILY.ToList());
}

这是视图标记:

@model IEnumerable<warehouse.Models.STATISTICS_DAILY>

@{
    ViewBag.Title = "Index";
    Layout = "~/Views/Shared/_LayoutDashboard.cshtml";
}

<table class="table">
    <tr>
        <th>
            @Html.DisplayNameFor(model => model.DATE)
        </th>
       

    </tr>

@foreach (var item in Model) {
    <tr>
      
        <td>
            @Html.DisplayFor(modelItem => item.DATE)
        </td>
       
     
        <td>
            @Html.ActionLink("Edit", "Edit", new { id = item.ID }) |
            @Html.ActionLink("Details", "Details", new { id=item.ID }) |
            @Html.ActionLink("Delete", "Delete", new { id=item.ID })
        </td>
    </tr>
}

</table>

但不是唯一的,这是输出

如何解决并仅选择不同的日期一次?

请注意,我已经尽量保持代码部分的原样性,但将其放在了翻译文本中以便理解上下文。如果您需要进一步的帮助,请随时提出。

英文:

I want to select distinct date but its not working and duplicated

This is the SELECT I tried it and its working in SQL Server :

SELECT DISTINCT date 
FROM STATISTICS_DAILY 
WHERE hospital_no = 1

This is the output I get:

2023-06-06 00:00:00.000
2023-06-07 00:00:00.000
2023-06-08 00:00:00.000
2023-06-08 15:21:46.000
2023-06-09 00:00:00.000
2023-06-10 00:00:00.000

But in ASP.NET MVC, how to get same result this is Index controller :

 public ActionResult Index()
 {
     int HospitalId = Convert.ToInt32(Session["HospitalID"]);

     var sTATISTICS_DAILY = db.STATISTICS_DAILY
                              .Where(x => x.hospital_no == HospitalId)
                              .OrderBy(d => d.DATE)
                              .Distinct()
                              .ToList();
     return View(sTATISTICS_DAILY.ToList());
 } 

This is the view markup :

@model IEnumerable<warehouse.Models.STATISTICS_DAILY>

@{
    ViewBag.Title = "Index";
    Layout = "~/Views/Shared/_LayoutDashboard.cshtml";
}

<table class="table">
    <tr>
        <th>
            @Html.DisplayNameFor(model => model.DATE)
        </th>
       
    
    </tr>

@foreach (var item in Model) {
    <tr>
      
        <td>
            @Html.DisplayFor(modelItem => item.DATE)
        </td>
       
     
        <td>
            @Html.ActionLink("Edit", "Edit", new { id = item.ID }) |
            @Html.ActionLink("Details", "Details", new { id=item.ID }) |
            @Html.ActionLink("Delete", "Delete", new { id=item.ID })
        </td>
    </tr>
}

</table>

but its not distinct this is the output

how to solve it and select distinct date one time only ?
在MVC中如何选择不同的日期?

答案1

得分: 1

你的Linq查询,如它所是,实际上没有按任何单一的不同列值来过滤行:在你的情况下,.Distinct() 步骤返回不同的输出,这些输出实际上不是 DateTime 值,而是表示源行的对象,这些对象本质上是不同的(因为内存中的对象 tend to be),因此为什么它没有进行过滤。

解决方案是更改你的查询,首先提取 date 值(这将使你的查询变成 IQueryable<DateTime> 而不是 IQueryable<STATISTICS_DAILY>;此外,因为 .Distinct() 是一个 reduction 操作,所以你将失去对 STATISTICS_DAILY 中的其他列的访问,这是我假设你已经熟悉的:

public async Task<ActionResult> Index(CancellationToken cancellationToken)
{
    Int32 hospitalId = Convert.ToInt32(this.Session["HospitalID"]); // 避免使用 Session-state
    List<DateTime> values = await this.db.STATISTICS_DAILY
        .Where(s => s.hospital_no == hospitalId)
        .Select(s => s.DATE)
        .Distinct()
        .OrderBy(dt => dt)
        .ToListAsync(cancellationToken);

    return this.View(values);
} 

你还需要将你的Razor .cshtml 文件的 @model 更改为 List<DateTime>

英文:

Your Linq query, as-is, doesn't actually filter rows by any single distinct column value: in your case the .Distinct() step returns distinct outputs, which in this case are not DateTime values but actually objects representing source rows, which are by nature distinct (as objects in-memory tend to be), hence why it isn't filtering.

The solution is to change your query to first extract the date value (which will turn your query into IQueryable<DateTime> instead of IQueryable<STATISTICS_DAILY>; also, because .Distinct() is a reduction operation you will lose access to the other columns in STATISTICS_DAILY, which is something I assume you're already familiar with:

public async Task<ActionResult> Index( CancellationToken cancellationToken )
{
    Int32 hospitalId = Convert.ToInt32( this.Session["HospitalID"] ); // Avoid using Session-state btw.
    List<DateTime> values = await this.db.STATISTICS_DAILY
        .Where( s => s.hospital_no == hospitalId )
        .Select( s => s.DATE )
        .Distinct()
        .OrderBy( dt => dt )
        .ToListAsync( cancellationToken );

     return this.View( values );
} 

You'll need to change your Razor .cshtml file's @model to List<DateTime> btw too.

huangapple
  • 本文由 发表于 2023年6月8日 12:00:38
  • 转载请务必保留本文链接:https://go.coder-hub.com/76428534.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定