Click here to Skip to main content
15,891,431 members
Please Sign up or sign in to vote.
4.00/5 (1 vote)
See more:
C#
public class Location_Master
    {
                
        public List<Locations> Locations { get; set; }
        public List<Response> Response { get; set; }
    }

    public class Locations
    {
        public int id { get; set; }
        public string lat { get; set; }
        public string longs { get; set; }
        public string country { get; set; }
        public string country_code { get; set; }
        public string name { get; set; }
        public int country_id { get; set; }
    }




C#
public void Locations(Location_Master lm)
        {
            DataTable _dt = new DataTable();
            _balL = new Safety_DAL.BALLocation();
                _dt = _balL.getAllLocations();
                lm.Locations = (from DataRow dr in _dt.Rows
                                select new Locations()
                                {
                                    id = Convert.ToInt32(_dt.Rows[0]["id"].ToString()),
                                    country = _dt.Rows[0]["country_name"].ToString(),
                                    country_code = _dt.Rows[0]["Country_ISD_Code"].ToString(),
                                    lat = _dt.Rows[0]["Location_Lat"].ToString(),
                                    longs = _dt.Rows[0]["Location_Long"].ToString(),
                                    name = _dt.Rows[0]["Name"].ToString(),
                                    country_id = Convert.ToInt32(_dt.Rows[0]["country_id"].ToString())
                                }).ToList();
                
                lm.Response = new List<Response>(){
                    new Response(){
                        response_code = 1,
                        success = true
                    }
                };
            }



output

"Locations": [
{
"id": 1,
"lat": "19.0760° N",
"longs": "72.8777° E",
"country": "India",
"country_code": "91",
"name": "Mumbai",
"country_id": 92
},
{
"id": 1,
"lat": "19.0760° N",
"longs": "72.8777° E",
"country": "India",
"country_code": "91",
"name": "Mumbai",
"country_id": 92
}
]

What I have tried:

i am getting the same record in in the list as above output, same id and other data too where as it's should be id 1 and id 2



C#
public class Location_Master
    {
        //public Int32 id { get; set; }
        //public string name { get; set; }

        //public virtual ICollection<Incident_Data> Incident_Data { get; set; }
        //public virtual User_Master User_Master { get; set; }
        public List<Locations> Locations { get; set; }
        public List<Response> Response { get; set; }
    }

    public class Locations
    {
        public int id { get; set; }
        public string lat { get; set; }
        public string longs { get; set; }
        public string country { get; set; }
        public string country_code { get; set; }
        public string name { get; set; }
        public int country_id { get; set; }
    }




C#
public void Locations(Location_Master lm)
        {
            DataTable _dt = new DataTable();
            _balL = new Safety_DAL.BALLocation();
                _dt = _balL.getAllLocations();
                lm.Locations = (from DataRow dr in _dt.Rows
                                select new Locations()
                                {
                                    id = Convert.ToInt32(_dt.Rows[0]["id"].ToString()),
                                    country = _dt.Rows[0]["country_name"].ToString(),
                                    country_code = _dt.Rows[0]["Country_ISD_Code"].ToString(),
                                    lat = _dt.Rows[0]["Location_Lat"].ToString(),
                                    longs = _dt.Rows[0]["Location_Long"].ToString(),
                                    name = _dt.Rows[0]["Name"].ToString(),
                                    country_id = Convert.ToInt32(_dt.Rows[0]["country_id"].ToString())
                                }).ToList();
                
                lm.Response = new List<Response>(){
                    new Response(){
                        response_code = 1,
                        success = true
                    }
                };
            }
Posted
Updated 5-Oct-16 5:12am

You're going to kick yourself when you see it :P

change
C#
id = Convert.ToInt32(_dt.Rows[0]["id"].ToString())

to
C#
id = Convert.ToInt32(dr["id"].ToString())


do that for each item in the linq query
 
Share this answer
 
Comments
nik varma 5-Oct-16 11:12am    
haha, yes
just found a link, but thank you so much and have given you a full rating :)
http://www.aspdotnet-suresh.com/2014/11/csharp-convert-datatable-to-generic-list-example-using-linq.html
C#
List<Locations> locations = new List<Locations>();

DataTableReader reader = dt.CreateDataReader();
if (reader != null && reader.HasRows)
{
    while (reader.Read())
    {
        locations.Add(new Locations()
        {
            // call the appropriate reader.GetXXXXX mthod that is appropriate for the property type
            id = reader.GetInt32("id"),
            ...
        });
    }
}


I wrote a bunch of extension methods for the DataTableReader that allows me to assign default values in case something came back wonky (yes, my data environment may produce wonky results, even from the database). I also access the reader by the column's ordinal value instead of by name so I can verify that the desired column actually exists in the table. An example:

C#
public static Int32 GetInt32OrDefault(this DataTableReader reader, int ordinal, Int32 defaultValue)
{
    Int32 value = defaultValue;
    if (!reader.IsDBNull(ordinal))
    {
        value = reader.GetInt32(ordinal);
    }
    return value;
}
 
Share this answer
 
v3

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900