none
Using LINQ to Filter DataTable RRS feed

  • Question

  • Hi,

    I have a Datatable with the below data...

    UserID	Mar  6 2012 	Mar  7 2012 
    ------------------------------------
    UserA	   100%		  100%
    UserB           28%               100%

    The columns are created dynamically at runtime - can be any number of dates.

    I want to create a filter that will return those rows with each date column containing "100%"

    So in this case only 'UserA' row data will be returned.

    Is this possible in LINQ?

    Thanks

    Wednesday, March 21, 2012 10:35 AM

Answers

  • Hi obrienkev,

    Welcome!

    I think you can dynamic count the number of Columns and multiply each row. if the result equel 1 return this row[0]. I think it is easy than LINQ.

    Have a nice day.


    Alan Chen[MSFT]
    MSDN Community Support | Feedback to us
    Get or Request Code Sample from Microsoft
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.



    Thursday, March 22, 2012 8:05 AM
    Moderator
  • For that you don't need even Linq the select does it for you

    dim rows = dt.Select("Month1=100 AND Month2 = 100") 'depending from the columntype of the row

    var rows = dt.Select("Month1=100 AND Month2 = 100"); //depending from the columntype of the row
    typed in this message

    Success
    Cor

    Monday, March 26, 2012 9:00 AM
  • If you are asking about LINQ to Dataset then here is the way:

                var rows = from r in table.AsEnumerable()
                           where r.Field<string>(column) == "100%"
                           select r;
    

    Cor's solution should work as well. The third options is similar to Cor's with the difference that you use a DataView over the given DataTable.


    Miha Markic [MVP C#] http://blog.rthand.com

    Wednesday, March 28, 2012 1:48 PM

All replies

  • Hi obrienkev,

    Welcome!

    I think you can dynamic count the number of Columns and multiply each row. if the result equel 1 return this row[0]. I think it is easy than LINQ.

    Have a nice day.


    Alan Chen[MSFT]
    MSDN Community Support | Feedback to us
    Get or Request Code Sample from Microsoft
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.



    Thursday, March 22, 2012 8:05 AM
    Moderator
  • For that you don't need even Linq the select does it for you

    dim rows = dt.Select("Month1=100 AND Month2 = 100") 'depending from the columntype of the row

    var rows = dt.Select("Month1=100 AND Month2 = 100"); //depending from the columntype of the row
    typed in this message

    Success
    Cor

    Monday, March 26, 2012 9:00 AM
  • If you are asking about LINQ to Dataset then here is the way:

                var rows = from r in table.AsEnumerable()
                           where r.Field<string>(column) == "100%"
                           select r;
    

    Cor's solution should work as well. The third options is similar to Cor's with the difference that you use a DataView over the given DataTable.


    Miha Markic [MVP C#] http://blog.rthand.com

    Wednesday, March 28, 2012 1:48 PM