Asp.net MVC foreach loop over db only returns 1 row

My Controller View

foreach (var dbItem in db1.Ranges.Where(x => x.DeviceId == Id).OrderByDescending(x => x.DeviceTime))
{
    list.Add((double)dbItem.Val1);
}

My Database rows --> Database Table

As you can see there are alot of rows in the DB, but when i run it i only get 1 row.

Upvotes: 0

Views: 1367

Answers (1)

smoksnes
smoksnes

Reputation: 10851

You can try:

var items = db1.Ranges
                 .Where(x => x.DeviceId == Id)
                 .OrderByDescending(x => x.DeviceTime)
                 .Select(x => x.Val1)
                 .ToList();

// if list is a List<double>
list.AddRange(items.Select(x => (double)x));

// If you really need the foreach
foreach(var value in items)
{
    list.Add((double)value);
}

Upvotes: 1

Related Questions