views:

66

answers:

3

Following Code for transmission to the list box is :

            DataTable dt = new DataTable();

        DataColumn dc = new DataColumn("BestSite", typeof(string));

        dt.Columns.Add(dc);

        for (int i = 1; i <= 10; i++)
        {
            DataRow dr = dt.NewRow();
            dr[0] = i.ToString() + " = stackoverflow";
            dt.Rows.Add(dr);
        }//EndFor

        var Query = from mycolumn in dt.AsEnumerable()
                    where mycolumn.Field<string>("BestSite") != string.Empty
                    select mycolumn;


        listBox1.DataSource = Query.AsDataView();

        listBox1.DisplayMember = "BestSite";

Transfer to array what should be? no loop

  string[] myvalue = new string[Query.AsDataView().Count];
A: 

If I understood your question correctly...

string[] myvalue = Query.Select(i => i.Field<string>("BestSite")).ToArray();
Fábio Batista
You can still do it with LINQ, check the answer again.
Fábio Batista
A: 

Error codes are )

string[] myvalue = (from i in Query.AsDataView() select i.BestSite).ToArray();

Error : Error 1 Could not find an implementation of the query pattern for source type 'System.Data.DataView'. 'Select' not found. Consider explicitly specifying the type of the range variable 'i'. c:\documents and settings\administrator\my documents\visual studio 2010\Projects\WindowsFormsApplication13\WindowsFormsApplication13\Form1.cs 40 43 WindowsFormsApplication13

string[] myvalue = Query.AsDataView().Select(i => i.BestSite).ToArray();

Error : Error 1 'System.Data.DataView' does not contain a definition for 'Select' and no extension method 'Select' accepting a first argument of type 'System.Data.DataView' could be found (are you missing a using directive or an assembly reference?) c:\documents and settings\administrator\my documents\visual studio 2010\Projects\WindowsFormsApplication13\WindowsFormsApplication13\Form1.cs 40 32 WindowsFormsApplication13

string[] myvalue = Query.AsDataView().Select(i => i.Field<string>("BestSite")).ToArray();

Error : Error 1 'System.Data.DataView' does not contain a definition for 'Select' and no extension method 'Select' accepting a first argument of type 'System.Data.DataView' could be found (are you missing a using directive or an assembly reference?) c:\documents and settings\administrator\my documents\visual studio 2010\Projects\WindowsFormsApplication13\WindowsFormsApplication13\Form1.cs 40 32 WindowsFormsApplication13

mystack
welcome to stackoverflow; it works a little different from regular forum threads. Please update your original question with additional info, and give the faq a quick read
STW
A: 

Finally realized

Correct answer :

     private string ConvertToString(DataRow dr)
    {
        return Convert.ToString(dr[0]);
    }

    private void button1_Click(object sender, EventArgs e)
    {

        DataTable dt = new DataTable();

        DataColumn dc = new DataColumn("BestSite", typeof(string));

        dt.Columns.Add(dc);

        for (int i = 1; i <= 10; i++)
        {
            DataRow dr = dt.NewRow();
            dr[0] = i.ToString() + " = stackoverflow";
            dt.Rows.Add(dr);
        }//EndFor

        //var Query = from mycolumn in dt.AsEnumerable()
        //            where mycolumn.Field<string>("BestSite") != string.Empty
        //            select mycolumn;

        DataRow[] myrow = new DataRow[dt.Rows.Count];
        dt.Rows.CopyTo(myrow, 0);

        string[] myString = Array.ConvertAll(myrow, new Converter<DataRow, string>(ConvertToString));


        foreach (string a in myString)
        {
            listBox1.Items.Add(a);
        }

    }
mystack