1

How can I convert DataTable.Rows collection to List<Category>?

I want to set the value of list of values of Data Row in to the List of values of C# object as I mentioned in the below code. kindly advice.

UserData objUserData = null;
DataTable dtUserData = DataAccess.getUserDataTable();  
if(dtUserData.Rows.Count>0)
{
    foreach(DataRow dr in dtUserData.Rows)
    {
      objUSerData = new UserData();
      List<objUSerData.Category> = Convert.ToString(List<dr["category"]>);
    } 
}

Here is the Category class:

[DataContract]
public class category { get; set; }
{
  public string category_id {get; set;}
  public string category_name {get; set;}
}
Reza Aghaei
  • 120,393
  • 18
  • 203
  • 398
user6410893
  • 53
  • 3
  • 9
  • `List = Convert.ToString(List);` is not valid by any stretch of the imagination. Could you show us the _compilable_ code you're actually using? Or, is this an attempt to show what you hope to accomplish? – theB Jun 06 '16 at 20:31
  • That's what I call a weird piece of code. What is it supposed to do? E.g. `Convert.ToString(List)` ?? – Stefan Steinegger Jun 06 '16 at 20:32
  • @StefanSteinegger I have a collection of category which is fetched from data row and how do i mention in the code syntactically correct? So i have put an approximate code over there. i stuck to complete syntax over there. – user6410893 Jun 06 '16 at 20:35
  • @theB I have a collection of category which is fetched from data row and how do i mention in the code syntactically correct? So i have put an approximate code over there. i stuck to complete syntax over there. – user6410893 Jun 06 '16 at 20:36
  • Ok, so the line there is an attempt to show what you want to do, but aren't sure about? Maybe you could add a few sentences above the code block describing the problem and put a comment in the code at that point so we know that that's what you intended. – theB Jun 06 '16 at 20:42
  • @StefanSteinegger i am getting error when i use select on data row as "Object does not contain a definition for 'Select' accepting a first argument of type 'object' could not be found(are you missing a assembly reference) – user6410893 Jun 06 '16 at 21:45

2 Answers2

0

Just guessing:

You probably want to create UserData from your rows:

foreach(DataRow dr in dtUserData.Rows)
{
   var userData = new UserData();
   // create an array with a single element
   userData.Categories = new [] 
   {
     new Category
     {
       category_id = ??,
       category_name = Convert.ToString(dr["category"])
     }
   };
   // not sure about this:
   userData.UserDetails = new [] 
   {
     new UserDetails
     {
       Whatever = Convert.ToString(dr["userdetails"]),
     }
   };
}

I actually don't know what kind of type dr["userdetails"] is.

Probably you need to map the rows to entities. These entities are probably found in another database. The category is probably already found there, and if not, it probably needs to be created. If so, everything is much more complicated.

The user data is most probably a more complicated record and I don't have a clue whats behind the field in the row.

Because there is no more information in the question, I can only guess.

Stefan Steinegger
  • 63,782
  • 15
  • 129
  • 193
  • UserData is my domain model which includes multiple list like category,user details, then how do i go about with that? – user6410893 Jun 06 '16 at 20:45
  • in your example, there is no relationship between userData object categorylist? – user6410893 Jun 06 '16 at 20:46
  • @user6410893: What do you want to do with these lists? – Stefan Steinegger Jun 06 '16 at 20:59
  • i want to transform the data row to my c# object and return the output as json to my client who consume the service. – user6410893 Jun 06 '16 at 21:01
  • You can't query against the DataTable's Rows collection, since DataRowCollection doesn't implement IEnumerable. You need to use the AsEnumerable() extension for DataTable -from the thread https://stackoverflow.com/questions/10855/linq-query-on-a-datatable?rq=1 – user6410893 Jun 06 '16 at 21:38
  • I think there isn't even a collection. You tried to convert category to a string. It's probably just a string. I'll update my answer. Don't know why there is a list in the UserData object. – Stefan Steinegger Jun 07 '16 at 05:51
0

You can create instances of category class using linq or a loop:

DataTable dtUserData = DataAccess.getUserDataTable();
var list = dtUserData.Rows.Cast<DataRow>()
                     .Select(row => new category()
                     {
                         category_id = row.Filed<string>("category_id"),
                         category_name = row.Filed<string>("category_name"),
                     }).ToList();

It's equivalent to:

var list = new List<category>();
foreach(DataRow row in dtUserData.Rows)
{
    list.Add(new category()
        {
            category_id = row.Filed<string>("category_id"),
            category_name = row.Filed<string>("category_name"),
        });
}
Reza Aghaei
  • 120,393
  • 18
  • 203
  • 398