Click here to Skip to main content
14,422,431 members
Rate this:
Please Sign up or sign in to 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 5 days ago
Rate this:
Please Sign up or sign in to vote.

Solution 1

newRow[]=t;

You need to specify which column to store the data:
newRow[0]=t; // store value in the first column
   
Comments
Member 14169626 5 days ago
   
But not 0. I want to first row title.Second row column 1 is count: column2 is 5
Richard MacCutchan 5 days ago
   
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 5 days ago
   
I want to count:5
Richard MacCutchan 4 days ago
   
Sorry, but that means nothing.
Maciej Los 5 days ago
   
5!
Richard MacCutchan 4 days ago
   
:)
Member 14169626 4 days ago
   
Col 1 Col2

Row 1 Tshirt 5

Row 2 Short 10

Row 3

.
.
.

Row 8 Shoes 6
Rate this:
Please Sign up or sign in to vote.

Solution 2

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.

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;
}
   

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




CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100