如何从数据表中获取数据到列表 [英] How to get a data from datatable to list

查看:69
本文介绍了如何从数据表中获取数据到列表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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; }
    }













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
                    }
                };
            }







输出



地点:[

{

id:1,

lat:19.0760°N,

longs:72.8777°E,

country:India,

country_code:91,

名称:孟买,

country_id:92

},

{

id:1,

lat:19.0760°N,

longs:72.8777°E,
country:India,

country_code:91,

name:Mumbai,

country_id:92

}

]



我尝试过:



i我在列表中获得与上面输出相同的记录,同样的id和其他数据,因为它应该是id 1和id 2 < br $> b $ b








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



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; }
    }













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
                    }
                };
            }

推荐答案

当你看到它时,你会踢自己:P



更改

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

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



to


to

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





为linq查询中的每个项目执行此操作



do that for each item in the linq query


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"),
            ...
        });
    }
}





我为DataTableReader编写了一堆扩展方法,允许我分配默认值万一有些东西回来了(是的,我的数据环境可能会产生不稳定的结果,即使是从数据库中也是如此)。我还通过列的序数值而不是名称来访问阅读器,因此我可以验证表中是否存在所需的列。例如:





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:

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


这篇关于如何从数据表中获取数据到列表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆