开发者

how convert DataTable to List<String> in C#

开发者 https://www.devze.com 2023-01-01 20:44 出处:网络
I am using C# Linq now I am converting DataTable to List and I am getting stuck... give me right direction thanks..

I am using C# Linq now I am converting DataTable to List and I am getting stuck... give me right direction thanks..

    private void treeview1_Expanded(object sender, RoutedEventArgs e)
    {

        coa = new List<string>();
        //coa = (List<string>)Application.Current.Properties["CoAFull"];
        HMDAC.Hmclientdb db = new HMDAC.Hmclientdb(HMBL.Helper.GetDBPath());

        var data = (from a in db.CoA
                    where a.ParentId == 0 && a.Asset == true
                    select new { a.Asset, a.Category, a.CoAName, a.Hide, a.Recurring, a.TaxApplicable });

        DataTable dtTable = new DataTable();
        dtTable.Columns.Add("Asset", typeof(bool));
        dtTable.Columns.Add("Category", typeof(string));
        dtTable.Columns.Add("CoAName", typeof(string));
        dtTable.Columns.Add("Hide", typeof(bool));
        dtTable.Columns.Add("Recurring", typeof(bool));
        dtTable.Columns.Add("TaxApplicable", typeof(bool));

        if (data.Count() > 0)
        {
            foreach (var item in data)
            {
                DataRow dr = dtTable.NewRow();
                d开发者_StackOverflowr["Asset"] = item.Asset;
                dr["Category"] = item.Category;
                dr["CoAName"] = item.CoAName;
                dr["Hide"] = item.Hide;
                dr["Recurring"] = item.Recurring;
                dr["TaxApplicable"] = item.TaxApplicable;
                dtTable.Rows.Add(dr);

            }
        }


        coa = dtTable;



    }


It seems that you already have a strongly typed list. Why converting this to a weakly typed DataTable and then back to a list?????

var data = 
    from a in db.CoA
    where a.ParentId == 0 && a.Asset == true
    select new 
    { 
        a.Asset, 
        a.Category, 
        a.CoAName, 
        a.Hide, 
        a.Recurring, 
        a.TaxApplicable 
    };
var list = data.ToList();

If you want to be able to use this list outside the scope of the method, define a type that will hold the different properties and in your select statement use this type instead of the anonymous type like:

var data = 
    from a in db.CoA
    where a.ParentId == 0 && a.Asset == true
    select new MyType
    { 
        Asset = a.Asset, 
        Category = a.Category, 
        CoAName = a.CoAName, 
        Hide = a.Hide, 
        Recurring = a.Recurring, 
        TaxApplicable = a.TaxApplicable 
    };
List<MyType> list = data.ToList();


You don't need the data table according to the code you have displayed:

var data = (from a in db.CoA 
    where a.ParentId == 0 && a.Asset == true 
    select new { a.Asset.ToString() + a.Category.ToString() 
        + a.CoAName.ToString()... }).ToList(); 


If you really want to convert your datatable to a 1D list, you can do it like this

foreach (DataRow row in dtTable.Rows) 
{
    foreach (DataColumn col in dtTable.Columns)
    {
        coa.Add(row[col]);
    }
}


As you are using Select new in you linq query It will find object. What you can do is

var data = (from a in db.CoA
                    where a.ParentId == 0 && a.Asset == true
                    select new { a.Asset, a.Category, a.CoAName, a.Hide, a.Recurring, a.TaxApplicable });

this is your query and you select multiple columns in your query. So you can't convert your data to a single List of string. What you can do is concatenate all the column in a single string and then add them in a list of string.

To do that modify your query like 'CK' said

var data = (from a in db.CoA 
    where a.ParentId == 0 && a.Asset == true 
    select new { a.Asset.ToString() + a.Category.ToString() 
        + a.CoAName.ToString()... }).ToList(); 

And then do

List<string> name  = new List<string>(data.ToList());
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号