Fastest way to fill DataTable from LINQ query using DataContext?

Your problem is this: as IEnumerable queryProjects = (IEnumerable)(from DataRow p in db. STREAM_PROJECTs.AsEnumerable() where p. Field("STREAM_ID") == StreamID select new { PROJECT_ID = p.

Field("PROJECT_ID"), PROJECT_NAME = p. Field("PROJECT_NAME") }) Except the version with as won't throw an exception when it fails to cast your query object (which is an IQueryable(this IEnumerable source) { PropertyInfo properties = typeof(T).GetProperties(); DataTable output = new DataTable(); foreach(var prop in properties) { output.Columns. Add(prop.Name, prop.

PropertyType); } foreach(var item in source) { DataRow row = output.NewRow(); foreach(var prop in properties) { rowprop. Name = prop. GetValue(item, null); } output.Rows.

Add(row); } return output; }.

Your problem is this: as IEnumerable The as keyword performs a safe cast, not a conversion, which it seems like you might think that it's doing. The as keyword is semantically the same as doing this: IEnumerable queryProjects = (IEnumerable)(from DataRow p in db. STREAM_PROJECTs.AsEnumerable() where p.

Field("STREAM_ID") == StreamID select new { PROJECT_ID = p. Field("PROJECT_ID"), PROJECT_NAME = p. Field("PROJECT_NAME") }); Except the version with as won't throw an exception when it fails to cast your query object (which is an IQueryable, where T is an anonymous type) to an IEnumerable (which it isn't).

Unfortunately, there is no built-in method that I'm aware of that will take an enumerable of a concrete type (like your anonymous type in this example) and turn it into a DataTable. Writing one wouldn't be too complicated, as you'd essentially need to get the properties reflectively then iterate over the collection and use those properties to create columns in a DataTable. I'll post an example in a few.

Something like this, placed in a static class within a namespace that you're using, should provide an extension method that will do what you want: public static DataTable ToDataTable(this IEnumerable source) { PropertyInfo properties = typeof(T).GetProperties(); DataTable output = new DataTable(); foreach(var prop in properties) { output.Columns. Add(prop. Name, prop.

PropertyType); } foreach(var item in source) { DataRow row = output.NewRow(); foreach(var prop in properties) { rowprop. Name = prop. GetValue(item, null); } output.Rows.

Add(row); } return output; }.

– JumpingJezza Feb 10 at 4:23 1 @JumpingJezza: Yes; as is to be used in cases where you know that the object might not actually be the type you're expecting and isn't an "exceptional" case. The as keyword evaluates to null if the cast is invalid, and (as a result) can only be used with reference types and Nullable. See the edit I just posted for a sample extension method that can turn any IEnumerable to a DataTable.

– Adam Robinson Feb 10 at 4:25 Perfect! Plus I found out something new about as :) – JumpingJezza Feb 10 at 4:39.

I am trying to run a linq query but I need the result as a datatable as I am using that to store records from different queries in the same viewstate object. The 2 versions below compile, but return an empty set. The exact error is "Value cannot be null.

I cant really gove you an answer,but what I can give you is a way to a solution, that is you have to find the anglde that you relate to or peaks your interest. A good paper is one that people get drawn into because it reaches them ln some way.As for me WW11 to me, I think of the holocaust and the effect it had on the survivors, their families and those who stood by and did nothing until it was too late.

Related Questions