LINQ to Objects Activity
This topic applies to Windows Workflow Foundation 4 (WF4).
This sample demonstrates how to create an activity to use LINQ to Objects to query elements in a collection.
Activity Details for FindInCollection
This activity allows users to query elements from collections in memory using LINQ to Objects. You must provide a LINQ predicate in the form of a lambda expression to filter the results. This activity can be used in conjunction with AddToCollection activities.
The following table details the property and return values for the activity.
Property or Return Value | Description |
---|---|
|
A required property that specifies the source collection. |
|
A required property that specifies the filter for the collection in the form of a lambda expression. |
Return Value |
The filtered collection. |
Code Sample that uses the Custom Activity
The following code example uses the FindInCollection
custom activity to find all rows in a collection of employees that have a Role
property set to Manager
and the Location
property set to Redmond
.
// Find all program managers in Redmond in the employees collection.
Activity wf = new FindInCollection<Employee>
{
Collections = new LambdaValue<IEnumerable<Employee>>(c => employees),
Predicate = new LambdaValue<Func<Employee, bool>>(c => new Func<Employee, bool>(e => e.Role.Equals("Manager") && e.Location.Equals("Redmond")))
};
The following code shows how to create a workflow program that uses the custom FindInCollection activity, AddToCollection, and ForEach activities to populate a collection with employees, find all the employees that have developer roles and are located in Redmond, and then iterate through the resulting list.
// Create the Linq predicate for the find expression
Func<Employee, bool> predicate = e => e.Role == "DEV" && e.Location.Equals("Redmond");
// Create workflow program
Activity sampleWorkflow = new Sequence
{
Variables = { employees, devsFromRedmond },
Activities =
{
new Assign<IList<Employee>>
{
To = employees,
Value = new LambdaValue<IList<Employee>>(c => new List<Employee>())
},
new AddToCollection<Employee>
{
Collection = new InArgument<ICollection<Employee>>(employees),
Item = new LambdaValue<Employee>(c => new Employee(1, "Employee 1", "DEV", "Redmond"))
},
new AddToCollection<Employee>
{
Collection = new InArgument<ICollection<Employee>>(employees),
Item = new LambdaValue<Employee>(c => new Employee(2, "Employee 2", "DEV", "Redmond"))
},
new AddToCollection<Employee>
{
Collection = new InArgument<ICollection<Employee>>(employees),
Item = new LambdaValue<Employee>(c => new Employee(3, "Employee 3", "PM", "Redmond"))
},
new AddToCollection<Employee>
{
Collection = new InArgument<ICollection<Employee>>(employees),
Item = new LambdaValue<Employee>(c => new Employee(4, "Employee 4", "PM", "China"))
},
new FindInCollection<Employee>
{
Collections = new InArgument<IEnumerable<Employee>>(employees),
Predicate = new LambdaValue<Func<Employee, bool>>(c => predicate),
Result = new OutArgument<IList<Employee>>(devsFromRedmond)
},
new ForEach<Employee>
{
Values = new InArgument<IEnumerable<Employee>>(devsFromRedmond),
Body = new ActivityAction<Employee>
{
Argument = iterationVariable,
Handler = new WriteLine
{
Text = new InArgument<string>(env => iterationVariable.Get(env).ToString())
}
}
}
}
};
To use this sample
Using Visual Studio 2010, open the LinqToObjects.sln solution file.
To build the solution, press CTRL+SHIFT+B.
To run the solution, press F5.
Note: |
---|
The samples may already be installed on your computer. Check for the following (default) directory before continuing.
<InstallDrive>:\WF_WCF_Samples
If this directory does not exist, go to Windows Communication Foundation (WCF) and Windows Workflow Foundation (WF) Samples for .NET Framework 4 to download all Windows Communication Foundation (WCF) and WF samples. This sample is located in the following directory.
<InstallDrive>:\WF_WCF_Samples\WF\Scenario\ActivityLibrary\Linq\LinqToObjects
|