dataTable form linq query

Posts   
 
    
mohamed avatar
mohamed
User
Posts: 136
Joined: 10-Mar-2008
# Posted on: 22-Jan-2010 17:49:04   

How to return dataTable form linq query ?

ex

LinqMetaData metaData = DataAccess.LinqMetaDataFactory.Create();

     var q = metaData.Snapshot.AsEnumerable<DataRow>().Select(s => s.someFields) as IEnumerable<DataRow>;
     return q.CopyToDataTable<DataRow>();
daelmo avatar
daelmo
Support Team
Posts: 8245
Joined: 28-Nov-2005
# Posted on: 23-Jan-2010 00:06:32   

Here is an example using projections:

[code]DataAccessAdapter adapter = new DataAccessAdapter();
LinqMetaData metaData = new LinqMetaData(adapter);

// the query
var q = metaData.Customers;

// execute linq query
EntityCollection<CustomerEntity> customers =
((ILLBLGenProQuery)q).Execute<EntityCollection<CustomerEntity>>();

// project results to DataTable
List<IEntityPropertyProjector> propertyProjectors =
    EntityFields2.ConvertToProjectors(customers.EntityFactoryToUse.CreateFields());

DataTable projectionResults = new DataTable();          
customers.DefaultView.CreateProjection(propertyProjectors, projectionResults);
David Elizondo | LLBLGen Support Team
mohamed avatar
mohamed
User
Posts: 136
Joined: 10-Mar-2008
# Posted on: 26-Jan-2010 15:31:28   

What about if my return statement didn't return from one table ?? ex

Select c.name, order.*,.... from customer as c join order on ... join...

MTrinder
User
Posts: 1461
Joined: 08-Oct-2008
# Posted on: 26-Jan-2010 21:20:53   

Add individual property projectors to the collection, rather than converting all of the fields in one go.


// project results to DataTable
List<IEntityPropertyProjector> propertyProjectors = new List<IEntityPropertyProjector>(); 

propertyProjectors.Add( new EntityPropertyProjector( OrderFields.OrderDate, "OrderDate" ) );
propertyProjectors.Add( new EntityPropertyProjector( CustomerFields.Name, "CustomerName" ) );


mohamed avatar
mohamed
User
Posts: 136
Joined: 10-Mar-2008
# Posted on: 26-Jan-2010 21:32:07   

MTrinder wrote:

Add individual property projectors to the collection, rather than converting all of the fields in one go.


// project results to DataTable
List<IEntityPropertyProjector> propertyProjectors = new List<IEntityPropertyProjector>(); 

propertyProjectors.Add( new EntityPropertyProjector( OrderFields.OrderDate, "OrderDate" ) );
propertyProjectors.Add( new EntityPropertyProjector( CustomerFields.Name, "CustomerName" ) );


How can execute this querey and its fields that comes from multiple tables

 // execute linq query
EntityCollection<CustomerEntity> customers =
((ILLBLGenProQuery)q).Execute<EntityCollection<CustomerEntity>>(); 

I dont want to go throw creating View I need direct way to get my return type as DataTable throw LinqMetaData ???

daelmo avatar
daelmo
Support Team
Posts: 8245
Joined: 28-Nov-2005
# Posted on: 27-Jan-2010 05:44:05   

Mmm. Sorry, there is not such method as "ToDataTable". You can write it yourself though, you can use Linq2Objects with the result anonymous type and some of these implementations: http://blogs.msdn.com/aconrad/archive/2007/09/07/science-project.aspx http://www.code-magazine.com/article.aspx?quickid=0707051&page=3

If this is very critical to you, you may consider using DynamicList as itself is a DataTable.

David Elizondo | LLBLGen Support Team