(C#) How can I extract Datatable when the value of a specific column belongs to the value in the list?

jun1.lee 21 Reputation points
2022-09-14T02:13:51.28+00:00

I want to extract only rows that fit certain conditions in the original datatable with C#.
Extracting rows that match one value were easy to find on the Internet and wrote the following code.

dt_thk = dt_raw.AsEnumerable()  
                 .Where(Row => Row.Field<string>("A") == "B")  
                 .CopyToDataTable();  

But I want to extract rows that are the values of "B", "C", "D", "E", "F". How do I make the code in this case? Thank you for any help.

Windows Presentation Foundation
Windows Presentation Foundation
A part of the .NET Framework that provides a unified programming model for building line-of-business desktop applications on Windows.
2,665 questions
C#
C#
An object-oriented and type-safe programming language that has its roots in the C family of languages and includes support for component-oriented programming.
10,190 questions
{count} votes

2 answers

Sort by: Most helpful
  1. Hui Liu-MSFT 37,946 Reputation points Microsoft Vendor
    2022-09-14T07:37:36.677+00:00

    Hi,@jun1.lee . You could try the code below to see if it helps you. For more information, you can refer the solution here.

    var res = from row in dt_raw.AsEnumerable()  
    where (row.Field<string>("A") == "B" ||  
    row.Field<string>("A") == "C" ||  
    row.Field<string>("A") == "D"||  
    row.Field<string>("A") == "E"||  
    row.Field<string>("A") == "F")  
    select row;  
    

    ----------------------------------------------------------------------------

    If the response is helpful, please click "Accept Answer" and upvote it.
    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.

    0 comments No comments

  2. MINGYU BAK 76 Reputation points
    2022-09-30T03:27:38.877+00:00

    246325-image.png

            //you can assign conditions flexibly  
            List<string> listTemp = new List<string> { };  
            listTemp.Add( "B" );  
            listTemp.Add( "C" );  
            listTemp.Add( "D" );  
            listTemp.Add( "E" );  
            listTemp.Add( "F" );  
    
            dt_thk = dt_raw.AsEnumerable( )  
                  .Where( Row => listTemp.Contains(Row.Field<string>( "A" )))  
                  .CopyToDataTable( );  
    

    246326-image.png

    0 comments No comments