1

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();
                dr["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;



    }
3
  • 2
    Please post the code you have written so far. People generally do not like to just write your code for you. Commented Jun 3, 2010 at 6:36
  • List<string> is a list of single string. But from you code above you are trying to store a series of values. Which field from the Linq do you want saved to the list. Commented Jun 3, 2010 at 6:47
  • this is good but my question is I want to convert List<string> coa = dtTable; How i can do this . Commented Jun 3, 2010 at 6:51

4 Answers 4

4

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();
Sign up to request clarification or add additional context in comments.

2 Comments

this is good but my question is I want to convert List<string> coa = data; How i can do this .
List<MyType> list = data.ToList(); this is not working.. i means i m getting error
2

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(); 

Comments

2

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

Comments

2

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());

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.