Click here to Skip to main content
15,885,767 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
I want to:

Counts Result
Count: 5



But this code result:

I want to title and count:5 --->rows

Row1: CountResult //TİTLE
Row2: Count:5


What I have tried:

public DataTale Count()
{
var t=MyStudentEntities.Where(x=>x.ID=10).Count();  ---> result:5

var dataTable=new System.Data.DataTable();
dataTable.Columns.Add("Count");
var newRow=dataTable.NewRow();
newRow[]=t;

dataTable.Row.Add(newRow);

}<
Posted
Updated 14-Jan-20 20:38pm

Your question is NOT clear. I'm guessing that you want to create new datatable. Then you want to add a number of rows depending on result of EF query.

C#
using System.Data;

public static DataTale CountResult(int _id)
{
var t=MyStudentEntities.Where(x=>x.ID=_id).Count();  //result:5

DataTable dt = new DataTable();
dt.Columns.Add(new DataColumn("CountResult", typeof(int)));
for(int i=0; i<=t; i++)
    dataTable.Row.Add(new object[]{i});

return dt;
}
 
Share this answer
 
C#
newRow[]=t;

You need to specify which column to store the data:
C#
newRow[0]=t; // store value in the first column
 
Share this answer
 
Comments
Member 14169626 14-Jan-20 14:35pm    
But not 0. I want to first row title.Second row column 1 is count: column2 is 5
Richard MacCutchan 14-Jan-20 15:21pm    
Sorry that is not very clear. What is in the three columns of Row1, and the first two columns of Row 2?
Row1:  ?    :   ?    :   ?
Row2:  ?    :   ?    :   5
Member 14169626 14-Jan-20 23:10pm    
I want to count:5
Richard MacCutchan 15-Jan-20 3:40am    
Sorry, but that means nothing.
Maciej Los 15-Jan-20 2:18am    
5!

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900