【解決方法】独自の foreach アクティビティを作成するか、データテーブル名のみを渡したい


以下の画像では、各ワークフロー アクティビティに組み込みシステムを使用しています。

しかし、データテーブル名の後に AsEnumerable を使用しないという要件は、データテーブル名を渡す必要があるだけですが、AsEnumerable を期待していることです。これはシステムアクティビティであるため、ワークフローでこの foreach ループアクティビティをどのように変更または作成できますか。

変更する場合はこれでも問題ありません。そうでない場合は、これと同様のカスタム ループ アクティビティを作成する必要があります。

私が試したこと:

システムアクティビティを変更しようとしましたができず、独自のアクティビティを作成する方法も見つかりませんでした。

解決策 1

To create a custom `foreach` activity in a workflow that accepts a `DataTable` without requiring the use of `AsEnumerable()`, 
you'll need to create a custom activity. Below is an outline of how you can achieve this:

1. **Create Custom Activity Class**:
   Create a new class that inherits from `CodeActivity` or `NativeActivity`, depending on your requirements.

2. **Define Input Argument**:
   Define an input argument of type `DataTable` to pass the `DataTable` to your custom activity.

3. **Implement Execution Logic**:
   Implement the execution logic of your custom `foreach` activity. You'll iterate over the rows of the `DataTable` directly without using `AsEnumerable()`.

4. **Add Custom Properties** (Optional):
   You can add custom properties to your activity to configure its behavior, such as filtering criteria or actions to perform on each row.

5. **Build and Use Custom Activity**:
   Build your custom activity project, add it to your workflow project, and then use it in your workflows.

Here's a simplified example of how you can create a custom `foreach` activity:


using System;
using System.Activities;
using System.Data;

namespace YourNamespace
{
    public class CustomForeachActivity : CodeActivity
    {
        [RequiredArgument]
        public InArgument<DataTable> InputDataTable { get; set; }

        protected override void Execute(CodeActivityContext context)
        {
            // Retrieve input DataTable
            DataTable dataTable = InputDataTable.Get(context);

            // Check if DataTable is not null
            if (dataTable != null)
            {
                // Iterate over DataTable rows
                foreach (DataRow row in dataTable.Rows)
                {
                    // Access row data and perform custom actions
                    string rowData = row["ColumnName"].ToString(); // Example: Access column "ColumnName"
                    Console.WriteLine(rowData); // Example: Output row data to console
                }
            }
        }
    }
}


What we have defined In this example:
- `CustomForeachActivity` is a custom activity class that inherits from `CodeActivity`.
- It has an input argument `InputDataTable` of type `DataTable`.
- In the `Execute` method, it retrieves the input `DataTable` and iterates over its rows directly.
- You can add additional logic inside the `foreach` loop to perform actions on each row of the `DataTable`.
- Build this custom activity project and use it in your workflows.

Once you've created and built your custom `foreach` activity, you can add it to your workflows and use it like any other activity, passing a `DataTable` to iterate over its rows without using `AsEnumerable()`.

コメント

タイトルとURLをコピーしました