Alan Alan - 2 months ago 6
ASP.NET (C#) Question

How can I convert a count returned from LINQ into a List?

In my code I am using this to do a count of rows in a table:

var phrasesCount = await db.Phrases.CountAsync();


I have on my front end a report viewer that expects to see data in the form of a List with Col1, Col2 etc and then the values assigned to each.

Ia there a way that I can make this .CountAsync() return it's value in the form of a list with one row with the value assigned to a field 'col1'?

Something like this:

.Select new { ?? };

Answer

How about:

var phrasesCount = await db.Phrases.CountAsync();
return new List<object>
{
    new 
    {
        Col1 = phrasesCount
    }
};