Ron Jacobs

Windows Workflow Foundation

How to use LINQ in a Workflow

How to use LINQ in a Workflow

Rate This
  • Comments 1

Question

Can I use a LINQ Query such as the following in a Workflow?

private static void ShowQueryWithCode(IEnumerable<string> names)
{
Console.WriteLine("Linq Query in Code - show names that start with 'R'");

// Assuming there are no null entries in the names collection
var query = from name in names where name.StartsWith("R") select name;

// This is the same thing as
// var query = names.Where(name => name.StartsWith("R"));


foreach (var name in query)
{
Console.WriteLine(name);
}

Console.WriteLine();
}


Answer

Yes, you can use LINQ with a Workflow in exactly the same way.

Download Sample Code

 

Step 1: Create a Workflow with an In Argument of type IEnumerable<string>

Here you can see that I've added the in argument

argument

Step 2: Add a Variable for the query of type IEnumerable<string>

Before you can add a variable you need to include an activity which has variables.  In this workflow I've added a sequence.

variable

Step 3: Assign the query the LINQ expression you want to use

You can use a method chain or query syntax.

expression

Step 4: Iterate over the collection

In the completed workflow I used a ForEach<string> activity to iterate the list of names and write them to the console.

Workflow1

Summary

This example uses C# in .NET 4.5 but the same technique can be used with Visual Basic.

When you run it you can see that both methods work the same.

console

Happy Coding!

Ron Jacobs
http://blogs.msdn.com/rjacobs
Twitter: @ronljacobs

  • Hi Ron,

    Sorry to post my question under this topic, as I am not sure where i can post my question without searching the blogs.

    Here i go..

    I am creating workflows dynamically for each project based on the tasks defined for projects in database.  Its working fine as long as i don't modify the tasks for any project like adding new task and removing the existing task for the workflow tree for a project. When I make changes to the workflow tree that time I see error msg like 'workflow tree is not matching with persisted instance'.

    To make it more clear:

    1. I am creating workflow pro-grammatically based on the tasks defined in database for a project.

    2. During the execution of project, I am adding new task or removing task from the that particular project in database

    3. When i resume and load the instance with new modified workflow tree schema, it throws error.

    My Question is :

    How can i handle such situation with same instance ID?

    Regards,

    Mahendra

Page 1 of 1 (1 items)