简体   繁体   中英

How to select some column in huge table which contain null data in some row and use distinict in column

I have huge table which contains some column null data but I don't want to change anything in table. In one of my view I want to select just two column to display in the page, first is ID and second is Area but in Area, I should use distinct and prevent to display null value there is my code but it not work and tell me how can I do it.

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 value=@itemArea.id>
                            @itemArea.areaname
                        </option>

                }
            </select>

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

Error Display

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]'.

Distinct will not prevent NULL values. It was not designed to do so. It will pull one NULL value with others.

If you want to skip the NULL values then why don't you just ignore those? Something like:

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

Update:

You are expecting list of school.Models.ClsArea in the view but you are sending list of DB.school type from action. DB.school is the entity model type right?

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