0

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 { ?? };
3
  • What are Col1, Col2 - is there some class with these as properties? If you just want to convert a single element to a sequence you can use Enumerable.Repeat or an initializer like new List<int>(){ phraseCount }.
    – Ian Mercer
    Oct 1, 2016 at 5:24
  • There is no class with the properties col1, col2 but I would like to make it so that data returned from the report method appears as a list with one or more of "col1", "col2" etc. On the front-end I can then take this data and put into a grid for display. I think I need something like you mention with List< .. but not with int as the return. For the return it would need to be an object with the property col1 and the value set to that. In this case the list would contain only one of those objects.
    – Alan2
    Oct 1, 2016 at 5:31
  • Check Google for DataTable and google.com/#q=create+list+of+objects+from+datatable+c-sharp - good luck! Oct 1, 2016 at 6:15

1 Answer 1

2

How about:

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

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.