2

I have the below code to return a list of strings.

public List<string> Top5CodesForToday()
{
    var date = DateTime.Now;
    var resultList = new List<string>();

    using (var db = new PillowContext())
    {
        var qry = (from d in db.DownTimes
            where DbFunctions.TruncateTime(d.DateTime) == DbFunctions.TruncateTime(date)
            group d by new {d.Code}
            into g
            let total = g.Sum(x => x.Amount)
            orderby total descending
            let top5 = g.Take(5).ToList()
            select new {g.Key.Code, Total = total});

        foreach (var item in qry)
        {
            int x = item.Code;
            var results = from r in db.DownTimeCodes
                          where r.Code == x
                          select r.Description;
            resultList.Add(results.ToString());
        }
    }
    return resultList;
}

When I look at the contents of returnList I am seeing the correct number of items however each item is made up of the actual query syntax, not the data itself. I have seen this before and usually solve it by doing .ToList() however I am unsure how I could change my code to solve this

2 Answers 2

7

The problem here is that when you are calling ToString the query is not executed yet, so essentially you are calling ToString on a IQueryable object, receiving the query instead of results. You need to call something to execute the query.

You can call ToList() still:

var results = (from r in db.DownTimeCodes
              where r.Code == x
              select r.Description).ToList();
resultList.AddRange(results);

Or, if you expect just one result, call FirstOrDefault()/SingleOrDefault():

var results = (from r in db.DownTimeCodes
              where r.Code == x
              select r.Description).FirstOrDefault();
resultList.Add(results);
Sign up to request clarification or add additional context in comments.

6 Comments

+1 There's no null check in the OP's code, so I'll wager that a call to .First() would be a better match in this case.
@ChristofferLette, not sure if this is an option at all, just added it for completeness. Name of the variable results hints that OP expects not one but several records
Correct, @Andrei. I'm just guessing, and I'm basing my guess on the fact that the OP is calling .ToString() on the results. If results is a list, .ToString() wouldn't normally be of much use.
Thanks Andrei, as simple as that. I have another problem now to due to running another query whilst iterating through the results from another query but that isn't related to the original question. I will mark as answer as soon as I have fixed that problem and tested
@Jimsan, just guessing of course, but it seems you can solve your another problem with calling another ToList for qry before the foreach.
|
1

You are calling ToString() on List<>. As default for most complex types, it just writes out type name not the data.

This line

resultList.Add(results.ToString());

should be changed to

resultList.AddRange(results);

3 Comments

I'm not sure that this will actually solve the OP's problem of getting rid of the "query syntax" in the results. I guess that what s/he wants to know is how to materialize the query.
OP explains that he got the right number of rows, but it contains only type names. So, the problems lies in filling of resultList. In sample provided both queries will be materialized - first by foreach, second - by adding items to resultList.
Ah... now I see what you did! And I didn't notice the switch to AddRange at first either.

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.