如何在巨大的表中选择某些列,其中某些行中包含空数据并在列中使用不同的字段

问题描述 投票:0回答:1

我有一个巨大的表,其中包含一些列空数据,但我不想更改表中的任何内容。在我的一个视图中,我只想选择两列显示在页面中,第一列是ID,第二列是Area,但在Area中,我应该使用distinct并防止显示空值有我的代码,但是它不起作用,请告诉我我该怎么做。

public class ClsArea
{
    public int id { get; set; }
    public string AreaName { get; set; }
}

public ActionResult Index()
        {


            return View(DB.school.ToList());
        }

----------



@model IEnumerable<school.Models.ClsArea>


<div class="container">
    <div class="row" style="margin-top:30px">
        <div class="col-lg-12 col-xs-12">
            <select class="form-control">
                @foreach (var itemArea in Model.Select(c => new { c.IdSMP, c.Area }).ToList() as IEnumerable<ClsArea>)
                {

                        <option [email protected]>
                            @itemArea.areaname
                        </option>

                }
            </select>

        </div>
    </div>
</div>

错误显示

The model item passed into the dictionary is of type 'System.Collections.Generic.List`1[<>f__AnonymousType1`2[System.Int32,System.String]]', but this dictionary requires a model item of type 'System.Collections.Generic.IList`1[PMIS_V01.Models.ClsArea]'.
c# asp.net-mvc model-view-controller view actionresult
1个回答
0
投票

Distinct不会阻止NULL值。它并非旨在这样做。它将与其他值一起拉一个NULL值。

如果要跳过NULL值,那么为什么不忽略它们呢?类似于:

DB.school.where(s => s.Area != null).ToList()

更新:

您希望在视图中显示school.Models.ClsArea的列表,但正在从操作发送DB.school类型的列表。 DB.school实体模型类型正确吗?

© www.soinside.com 2019 - 2024. All rights reserved.