Var query1.=the from t in dt_return_all_x1 AsEnumerable ()
Group t by new
{
T1=t.F ield(" ID "production batches), t2=t.F ield (" ID ")
}
Into m
Select new
{
Production batch number ID=m.K ey. T1, product ID=m.K ey. T2, number=Margaret spellings um (n=& gt; N.F ield(" number "))
};
I use the following method converts the datatable,
The DataTable dt=ToDataTable (query1. ToList ());
Public DataTable ToDataTable(List Entitys)
{
////check collection entity can't be empty
If (entitys==null | | entitys. Count & lt; 1)
{
Throw new Exception (" need to convert collection is empty ");
}
//remove the first entity all Propertie
Type entityType=entitys [0]. GetType ();
PropertyInfo [] entityProperties=entityType. The GetProperties ();
//generate DataTable structure
The DataTable dt=new DataTable ();
for (int i=0; I & lt; EntityProperties. Length; I++)
{
Dt. Columns. The Add (entityProperties [I]. Name, entityProperties [I] PropertyType);
}
//add all of the entity to the DataTable
Foreach (object entity in entitys)
{
Object [] entityValues=new object [entityProperties Length].
for (int i=0; I & lt; EntityProperties. Length; I++)
{
EntityValues [I]=entityProperties [I] GetValue (entity, null);
}
Dt. Rows. The Add (entityValues);
}
Return dt.
}
If not empty set have records of the linq query, can return to normal a datatable,
If linq queries to the results, and it throws an exception,
How do I do it: when the linq query is an empty set, you can generate a field, only empty DataTable without record?
Production batch number ID product ID number
CodePudding user response:
Set the Count is zero, also create the DataTable can, if there is data, insert data to the DataTableCodePudding user response:
Set the count of 0,//remove the first entity all Propertie
Type entityType=entitys [0]. GetType ();
This code will be out of the question, "index beyond the scope of"
CodePudding user response:
Type entityType=entities. GetType (). GetGenericArguments () [0];So we can get type, regardless of the entities have the data
CodePudding user response: