-1

I am new to LINQ and am trying to create a collection say "DBDatalog" of only one column DCID and then converting that collection to datatable.

 var DBDatalog = from records in this.Points
                                          where records.UsesLegacyStorage == false
                                          select new
                                          {
                                              DcId = records.DCId,
                                          };

I want to be able to do something like this

DataTable dt = new DataTable();
dt.Columns.Add("DCId", typeof(Int32));

AND for the contents of DataTable

DataTable dataTable = DBDatalog.toDataTable();

But that is not working. How can I convert my collection set attained in the dbdatalog to convert to data table.

Points is a list of objects defined like this public List Points = new List(); and Exports is defined like this.

public class ExportPoint
    {
        public int DCID;
        public int BuildingId;
        public string BuildingName;
}
Mahyar Mottaghi Zadeh
  • 1,178
  • 6
  • 18
  • 31
Sarah
  • 1,199
  • 2
  • 21
  • 42
  • What is in records? What is in DBDatalog? Also please give a better problem decription then "not working". – Christopher Aug 05 '19 at 22:36
  • @Christopher, thanks much By not working I mean, toDataTable is not defined. I need a way to convert DBDataLog to DataTable. DBDatalog is defined as a var in code above. – Sarah Aug 05 '19 at 22:43
  • Have a look at this: https://stackoverflow.com/questions/564366/convert-generic-list-enumerable-to-datatable – Harsh Aug 05 '19 at 22:49
  • @Sarah: IIRC, LINQ uses a Enumeartor. Or rather the result is a Enumerator. You can just ittearte it over in a foreach loop and add something similar to each element to the DataTable. – Christopher Aug 05 '19 at 22:57
  • DataTable dataTable = DBDatalog.AsEmumerable().CopyToDataTable(); – jdweng Aug 05 '19 at 23:20
  • @Harsh, I looked up that link and didnt help me much. Like I said I am new to Linq and collections and that link on stackoverflow was not much helpful. – Sarah Aug 05 '19 at 23:56
  • @jdweng trying that. Will let you know soon. – Sarah Aug 05 '19 at 23:56
  • @jdweng this is the error I am getting when I follow your suggestion.. 'IEnumerable<<>f__AnonymousType0>' does not contain a definition for 'AsEmumerable' and no accessible extension method 'AsEmumerable' accepting a first argument of type 'IEnumerable<<>f__AnonymousType0>' could be found (are you missing a using directive or an assembly reference?) – Sarah Aug 05 '19 at 23:59
  • `foreach (var a in DBDatalog) dt.Rows.Add(a.DcId);` – Alexander Petrov Aug 06 '19 at 02:53

1 Answers1

1

After searching for hours for an answer which is most uncomplicated and straight forward. This is what I found, I am placing it here in case it helps someone else.

string json = Newtonsoft.Json.JsonConvert.SerializeObject(DBDatalog);

The above line will give you a string like this "[{\"DcId\":755},{\"DcId\":7482},{\"DcId\":490}]"

DataTable dt = new DataTable();
dt = JsonConvert.DeserializeObject<DataTable>(json);

This works like a charm, no need to write extension methods and all that stuff.

Thanks to this answer very clean and to the point. https://stackoverflow.com/a/34202937/7817343

Sarah
  • 1,199
  • 2
  • 21
  • 42