none
Filter columns from a based on a column which is dynamically generated within the DataTable using LINQ RRS feed

  • Question

  • Hi,

    I have a DataTable as follows. Now my requirement is ,I need to filter columns where value is appearing as "Yes" and copy it into another DataTable.

    elements

    P1A3431223

    P2A23423424

    Yes

    Yes

    Fn

    0.23

    .33

    Za

    .566

    .24

    K

    .4353

    .34123

    Mn

    .231

    .34

    In the above DataTable elements,P1A3431223 and P2A23423424 are the column headers which are dynamically generated at runtime. Now I want to filter only columns where value "Yes" appears and copy into another datatable. I would like to use LINQ for this.Can someone please suggest me how to do this.

    Regards

    -Pep

    Saturday, March 3, 2012 4:43 AM

Answers

  • Hi pepcoder;

    This can be done as follows:

    var results = from r in dt.AsEnumerable()
                  where r.ItemArray.Any (v => v == "Yes" )
                  select r; 
    
    DataTable dt2 = results.CopyToDataTable();
    Where dt in the above query is the original DataTable object.

       


    Fernando (MCSD)

    If a post answers your question, please click "Mark As Answer" on that post and "Mark as Helpful".


    Saturday, March 3, 2012 2:56 PM