Marcin Borowski
Marcin Borowski

Reputation: 13

Filtering .ToList()

i'm new to ASP.NET MVC. What I have: - database with one table which contains production data in following columns: machine_name, good_parts, bad_parts, date.

Based on MS tutorial to ASP.NEt MVC5 i have build simple application to display data from database.

What I want: I want to filter the results by machine_name, because there is too many records in DB, and browser hangs while rendering results.

Controller looks like that:

public ActionResult Index()
        {
           return View(db.DATABASE.ToList());

        }

View page:

<table class="table">
    <tr>
        <th>
            @Html.DisplayNameFor(model => model.machine_name)
        </th>
        <th>
            @Html.DisplayNameFor(model => model.date)
        </th>
        <th>
            @Html.DisplayNameFor(model => model.good_parts)
        </th>
        <th>
            @Html.DisplayNameFor(model => model.bad_parts)
        </th>
    </tr>

@foreach (var item in Model)
{
    if (item.machine_name == "machine_1")
    {
      <tr>
        <td>
            @Html.DisplayFor(modelItem => item.machine_name)
        </td>
        <td>
            @Html.DisplayFor(modelItem => item.data)
        </td>
        <td>
            @Html.DisplayFor(modelItem => item.good_parts)
        </td>
        <td>
            @Html.DisplayFor(modelItem => item.bad_parts)
        </td>
      </tr> 
     }
}

After implementing if statement, view does not return any records from DB. Thanks a lot in advance for your time explaining me the basics.

Marcin.

Upvotes: 0

Views: 1771

Answers (2)

DATTA SAI KRISHNA
DATTA SAI KRISHNA

Reputation: 7

You can try this:

db.DATABASE.Where(item=>item.machine_name =="machine1").ToList();

Upvotes: -2

TheGeneral
TheGeneral

Reputation: 81493

Its as easy as

db.DATABASE.Where(x => x.MachineName == "something").ToList()

or

db.DATABASE.Take(100).ToList()

Enumerable.Where Method

Queryable.Where Method

Filters a sequence of values based on a predicate.

Enumerable.Take(IEnumerable, Int32) Method

QueryableExtensions.Take Method (IQueryable, Expression>)

Returns a specified number of contiguous elements from the start of a sequence

Upvotes: 4

Related Questions