sai kumar

sai kumar

  • NA
  • 14
  • 16.1k

How to map DataTable to DTO using ExpressMapper?

Oct 12 2016 3:03 AM

Iam using asp.net webapi ,c# as i am new to expressmapper i have ado.net code which is returning list of reults.then how to map using expressmapper.searched so many sites but unfortunately no results found can u help in it


  1. public class HomeController : ApiController  
  2.    {  
  3.        [HttpGet]  
  4.        [AllowAnonymous]  
  5.        public List<CustomerViewmodel> Newmethod()  
  6.        {  
  7.            string connectionString =  
  8.                "server = sasd; database = das; uid = ds; pwd = Welcome@123; ";  
  9.            // Provide the query string with a parameter placeholder.  
  10.            string queryString =  
  11.                "SELECT distinct TOP 5 ISNULL(C.Name, 'Others') AS 'Company',A.Name AS 'Customers', O.Fee AS 'Amount' FROM   ClientCursor.Opportunity AS O INNER JOIN Common.Company AS C ON C.Id = O.ServiceCompanyId INNER JOIN [ClientCursor].[Account] AS A ON A.Id = O.AccountId order by  O.Fee DESC";  
  12.            // Create and open the connection in a using block. This  
  13.            // ensures that all resources will be closed and disposed  
  14.            // when the code exits.  
  15.            using (SqlConnection connection =  
  16.                new SqlConnection(connectionString))  
  17.            {  
  18.                // Create the Command and Parameter objects.  
  19.                SqlCommand command = new SqlCommand(queryString, connection);  
  20.                connection.Open();  
  21.                SqlDataAdapter da = new SqlDataAdapter(command);  
  22.                DataSet ds = new DataSet();  
  23.                da.Fill(ds, "newtable");  
  24.   
  25.                Mapper.Register<DataRow, CustomerViewmodel>().Flatten();  
  26.                yield return Mapper.Map<DataRow, CustomerViewmodel>(ds);  
  27.                  
  28.            }  
  29.        }  
  30.    } 



Answers (2)