Linq to dataset - select query question RRS feed

  • Question

  • Hello,

    I want to do a simple query from a dataset table as -


    Database Table Name - ABC

    ColumnNames - Col1,Col2,Col3, Col4, Col5

    select (col1 +Col2) as MyCol, * from abc.

    Now when I do the same with LINQ, instead of usin * i need to provide all the column names. Is there a way

    from o in myDataSet.Tables["abc"].AsEnumerable()
           select new {MyCol = o.Field<string>("Col1")+o.Field<string>("Col2"),
         Col1= o.Field<string>("Col1"),
    Col1= o.Field<string>("Col1"),
    Col2= o.Field<string>("Col2"),
    Col3= o.Field<string>("Col3"),
    Col4= o.Field<string>("Col4"),
    Col5= o.Field<string>("Col5")


    which becomes a headache, if i have 50 columns. Is there a way I can abc.*

    any suggestions are welcom




    Tuesday, August 17, 2010 5:20 PM


  • Hi babooraj,

    I think there is no direct way to do that. But you can implement in another direction. For example:

    var result = from o in myDataSet.Tables["abc"].AsEnumerable()
           select o;
    DataTable dat = result.CopyToDataTable();
    dat.Columns.Add("Col1AndCol2").Expression = "Col1 + Col2"; 
    this.dataGridView1.DataSource = dat; 

    Best regards,
    Alex Liang

    Please remember to mark the replies as answers if they help and unmark them if they provide no help.
    Welcome to the All-In-One Code Framework! If you have any feedback, please tell us.
    Thursday, August 19, 2010 3:01 AM