将数据表转换为datalist [英] convert data table to datalist

查看:91
本文介绍了将数据表转换为datalist的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

你好是我的代码。

我在数据表中从数据库返回数据。在将数据表转换为列表时获得优惠







Hi belo is my code.
I m returning data from database in data table. and getting an excetion of while converting data table to list



Unable to cast object of type 'System.Data.DataRow' to type 'JSONWebService.t_country_cmn'.




[WebMethod]
        public List<t_country_cmn> getcountrybyid()
        {
            DataTable dt = _obj.CountryList();
            List<t_country_cmn> rows = dt.Rows.Cast<t_country_cmn>().ToList();
            return rows;
        }</pre>









以下是我的样本json数据返回网络服务:



{

getsearchresultdataResult:[

{

address_text:indore54 vijaynagar,

city_sk:19,

country_name:India,

email_id :dheerajgoojar31@gmail.com,

phone_no:,

postal_code:2342,

service_provider_name :皮肤发光,

service_provider_sk:22759,

state_sk:13

},

{

address_text:lkjlksjd lkjlksdf,

city_sk:19,

country_name:India,

email_id:dheeraj_goojar31 @ yahoo.com,

phone_no:,

postal_code:4660021,

service_provider_name:skinrise,

service_provider_sk:22769,

state_sk:13

}

]

}





Following is my sample json data returned from web service:

{
"getsearchresultdataResult": [
{
"address_text": "indore54 vijaynagar",
"city_sk": 19,
"country_name": "India",
"email_id": "dheerajgoojar31@gmail.com",
"phone_no": "",
"postal_code": "2342",
"service_provider_name": "skin glow ",
"service_provider_sk": 22759,
"state_sk": 13
},
{
"address_text": "lkjlksjd lkjlksdf ",
"city_sk": 19,
"country_name": "India",
"email_id": "dheeraj_goojar31@yahoo.com",
"phone_no": "",
"postal_code": "4660021",
"service_provider_name": "skinrise",
"service_provider_sk": 22769,
"state_sk": 13
}
]
}

推荐答案

Hi Avinash,



搭配就像你正在将数据表转换为列表一样。



所以,这里 List< t_country> 是一个类型为 t_country_cmn 的列表。



现在,t_country_cmn即将被创建。



一般来说,List< object>是一个动态数组。使用List,您无需自行管理大小。列表被视为泛型和构造类型。你需要使用<和>在列表声明中。列表处理任何元素类型(从字符串到类)。



因此,根据数据表列,您需要创建t_country_cmnclass。



然后将行数添加为列表大小。



Hi Avinash,

Looks like you are converting the datatable to a list.

So, here List<t_country> is a list of type t_country_cmn.

Now, t_country_cmnhas to be created.

Generally, List<object> is a dynamic array. With List, you do not need to manage the size on your own. Lists are considered as generics and constructed types. You need to use < and > in the List declaration. Lists handle any element type (starting from a string to a class).

So, depending upon the datatable columns, you need to create the t_country_cmnclass.

Then add the number of rows as list size.

var lstGridData = new List<t_country>(dt.Rows.Count);





然后,您需要将dt中的数据传递到列表中,如下面的代码:





Then, you need to pass on the data in the "dt" into the list, like bellow code:

lstGridData.AddRange(from DataRow row in dt.Rows
                                     select
                                         new t_country_cmn(row[column0], row[column1], row[column2], ....);





根据列数,应在此处创建t_country类。



现在,您必须返回此列表对象,而不是返回行





如果您想了解更多信息,请告诉我。请提供什么重新列出dt中的列以获得更好的解释。



谢谢,

RelicV



Depending upon the number of columns, the t_country class should be created here.

now, you have to return this list object, instead of the return rows.


Let me know if you want more info. Do please provide what are the columns in dt for better explanation.

Thank you,
RelicV


这篇关于将数据表转换为datalist的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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