1

I want to make a generalized Data Table to Linq Collection I am a beggginer so if it's not possible please let me know

public void Something(DataTable dt)
{
    var data = from row in dt.AsEnumerable()
               select new { 
                            Order = row["Order"].ToString(), 
                            Something = row["Something"].ToString(),
                            Customer = row["Customer"].ToString(),
                            Address = row["Address"].ToString()
                          };
}

That is the code for one table i want something like this:

    public static void convertDatatable(DataTable dt)
    {
        var results = from myRow in dt.AsEnumerable()
                      select new
                      {
                          foreach(DataColumn column in dt.Columns)
                              column.ColumnName // linq Variable name
                                  = myRow[column.ColumnName];// linq Variable Value
                      };
    }

I know it doesn't work how i wrote it but is there another way ?

Note: the reason i am doing this is because i can't convert Datatable directly to JSON it serializes it to XMl then sends it as a string containing that xml.

Shereef Marzouk
  • 3,282
  • 7
  • 41
  • 64
  • possible duplicate of [How can I convert a DataTable into a Dynamic object?](http://stackoverflow.com/questions/7794818/how-can-i-convert-a-datatable-into-a-dynamic-object) – Ani Mar 06 '12 at 14:20
  • @Ani - it's not a duplicate - this is being asked because he wants to do JSON serialization, and a dynamic object won't help there. – Andras Zoltan Mar 06 '12 at 14:33
  • possible duplicate of [What should I use to serialize a DataTable to JSON in ASP.NET 2.0?](http://stackoverflow.com/questions/8592294/what-should-i-use-to-serialize-a-datatable-to-json-in-asp-net-2-0) – Andras Zoltan Mar 06 '12 at 14:44

1 Answers1

2

If you want to stay with datatables then there is this, mentioned in another SO: What should I use to serialize a DataTable to JSON in ASP.NET 2.0?, which links to What should I use to serialize a DataTable to JSON in ASP.NET 2.0?.

I highly recommend, however, that you consider moving away from DataTables and DataRows, replacing it instead with an ORM such as Entity Framework (EF Quick Start here) or Linq to Sql - there are others, but since you are a beginner these offer the easiest learning curve; not least because of the full designer support in Visual Studio.

For the standard forms of JSON serialization offered by .Net (e.g. WCFs DataContractSerializer or the Asp.Net JSON serializer) then you need concrete types. The ORM solution will create all your table wrapper types at design-time, giving you a concrete type, potentially, for every table in your database.

As for the idea you've specifically outlined above, it is exceptionally difficult to achieve - because the compiler, in the first example, dynamically generates a type whose members match the names and types of the expressions you use. If you open your compiled code in ILSpy and switch to IL instead of C# you'll see what I mean.

Therefore, to reproduce it dynamically you would need to dynamically emit a class, probably using ILGenerator, doing the same thing; and then dynamically emit the expression tree (using the Expression class' static factory methods) to fill it out; and finally compile and execute it.

I would only look at doing something like that if I literally couldn't do it any other way - I'd be more likely to just write a routine to iterate through each column and write the JSON to a StringBuilder and return that! But if I could use an ORM, then I'd do that instead.

Community
  • 1
  • 1
Andras Zoltan
  • 41,961
  • 13
  • 104
  • 160
  • I am sorry but either you forgot how beginners are or i am too much of a beginner, i am lost lost lost in your answer, i feel like if someone else reads it they will reach their goal but i am just lost, I've seen Entity framework queries that return JSON but since i am a beginner and i don't know how to use EFW, can a datatable be converted to Entity framework ? it would be too much of an over head for me to learn entity framework and redesign my webservice using it,and i couldn't even know where to start with the Classes u linked,if you can please elaborate more, thanks.P.S.:speed is an issue – Shereef Marzouk Mar 06 '12 at 21:48
  • When i say speed is an issue i mean that the time i takes the code to run to reach JSON is important, i don't mean to rush you, I already reworked my code to use JSON.NET library but it's just slower than the built in serializer when converting a big datatable – Shereef Marzouk Mar 06 '12 at 21:51
  • @Shereef - Hi there - I understand what you're saying, but unfortunately to do *exactly* what you've asked is indeed a complicated thing and probably something that a beginner shouldn't be attempting, I personally would shy away from it because of it's complexity, and I'm comfortable with generating dynamic code and expression trees! If you use EF or L2S you don't use datables any more - all tables are wrapped in strongly-typed classes, making it far easier. I recommend the EF quick start http://msdn.microsoft.com/en-us/library/bb399182.aspx - it will teach you a lot. – Andras Zoltan Mar 07 '12 at 08:30