question

jun1lee-3451 avatar image
0 Votes"
jun1lee-3451 asked MINGYUBAK-1764 edited

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

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.

dotnet-csharpwindows-wpf
· 1
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

Hi, @jun1lee-3451 . Is there any update to the question? Did the answer below solve your problem? If so, you could accept it as the answer. It's helpful for community members with related questions.

0 Votes 0 ·
HuiLiu-MSFT avatar image
0 Votes"
HuiLiu-MSFT answered HuiLiu-MSFT edited

Hi,@jun1lee-3451. 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.


5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

MINGYUBAK-1764 avatar image
0 Votes"
MINGYUBAK-1764 answered MINGYUBAK-1764 edited

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



image.png (3.6 KiB)
image.png (2.6 KiB)
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.