0

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.

1
  • Maybe a stupid question, but are you sure that you're quering the same DB as the screenshot? And if so, which Val1 are you getting? And are you using DevideID 162201083007044? Commented Jun 16, 2016 at 12:35

1 Answer 1

1

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);
}
Sign up to request clarification or add additional context in comments.

3 Comments

Care to elaborate a bit why we need items.Select(x => (double)x) ? I mean, I couldn't really see the difference. Just wondering if I'm missing something.
Are you pointing out the fact that I moved the cast? It's an old habit from my side. I always try to avoid too much logic in the query. But I'm sure it can be done in the query as well. Also, I added .ToList() in to query the DB before we iterate it.
Ah, now I see. I missed .ToList() was missing in OP. Nice catch!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.