0

I have used the following code:

ManageUser userObj = new ManageUser();
List<StoreUserList> storeList = new List<StoreUserList>();
StoreUserList sl = new StoreUserList();
foreach (var items in userObj.GetStoreUserList(Username, Password))
{
    sl.UserName = items[0].ToString();
    sl.EmailId = items[1].ToString();
    sl.FirstName = items[2].ToString();
    sl.LastName = items[3].ToString();
    sl.BadLoginCount = Convert.ToInt32(items[4]);
    sl.ManagerName = items[5].ToString();
    storeList.Add(sl);
}

StoreUserList is my Model in which i have defined all the properties.
ManagerUser is the class which will return the data in the form of List<DataRow>.

So, i order to populate the a generic list of my model type i have used the above code, to do it.
But, as you can see i have hard-coded all the values to bind the model and then added that model to the list. I just wanted to know that is there some other way to do it or some magic way to do it in easy?

HarshSharma
  • 630
  • 3
  • 9
  • 34
  • 1
    We can do it using reflection and LINQ as given [here][1] [1]: http://stackoverflow.com/questions/12662318/how-to-convert-datatable-to-listt-using-reflections – Satheesh Feb 05 '14 at 06:17
  • 1
    @Harsh: I don't think your code would work. Would it? Your storeList might only contain the same row unless you move the line where "sl" is defined into the loop. – Johnny Feb 05 '14 at 06:27
  • 1
    Your issue there is going to be that Convert.ToInt32. Creating a general method to convert a DataRow to a custom type would normally rely on the field values in the DataRow being the correct type for the properties of the object. You can probably employ the Convert.ChangeType to perform that conversion in a more generalised way. – jmcilhinney Feb 05 '14 at 06:29

3 Answers3

1

You can use automapper project for this. See more details here:

How to use AutoMapper to map a DataRow to an object in a WCF service?

And here: Map RowDataCollection to DTO using AutoMapper

Community
  • 1
  • 1
Tony
  • 7,345
  • 3
  • 26
  • 34
1

I think your code will not add multiple StoreUserList object in the list but will add only 1 object with the value as last datarow. What you need to do is

        ManageUser userObj = new ManageUser();
        List<StoreUserList> storeList = new List<StoreUserList>();
        StoreUserList sl = null;
        foreach (var items in userObj.GetStoreUserList(Username, Password))
        {
            sl = new StoreUserList();

            sl.UserName = items[0].ToString();
            sl.EmailId = items[1].ToString();
            sl.FirstName = items[2].ToString();
            sl.LastName = items[3].ToString();
            sl.BadLoginCount = Convert.ToInt32(items[4]);
            sl.ManagerName = items[5].ToString();
            storeList.Add(sl);
        }

Otherwise i dont see any problem in your code.

samar
  • 5,021
  • 9
  • 47
  • 71
1

If you can modify ManageUser.GetStoreUserList a little to return DataTable instead of List<DataRow>, then you can use Automapper, which can map IDataReader implementations to a collection of objects.

Suppose you have this entity type:

class User
{
    public int Id { get; set; }
    public string Name { get; set; }
}

Then you can convert DataTable to the List<User> this way:

        // sample data table
        var dataTable = new DataTable();

        dataTable.Columns.Add("Id", typeof(int));
        dataTable.Columns.Add("Name", typeof(string));

        dataTable.Rows.Add(1, "John");
        dataTable.Rows.Add(2, "Mary");
        dataTable.Rows.Add(3, "Peter");
        dataTable.Rows.Add(4, "Sarah");

        // fill users list
        using (var reader = dataTable.CreateDataReader())
        {
            var users = Mapper.Map<IDataReader, List<User>>(reader);
        }
Dennis
  • 37,026
  • 10
  • 82
  • 150
  • i ain't able to find what is `Mapper` here, because its not coming in my VS when i type it – HarshSharma Feb 05 '14 at 06:38
  • Automaper is a library, which can be installed as NuGet package. Right-click on project in solution explorer window, choose "Manage NuGet packages", find Automapper and add it to the project. – Dennis Feb 05 '14 at 06:49
  • Mapper.DynamicMap is obsolete. [Use the code of this answer instead](https://stackoverflow.com/a/48821184/4266998) –  Jun 07 '18 at 12:26