score:1
You can't make your own ParallelQuery<T>
based classes unfortunately due to the fact that while ParallelQuery<T>
is public it does not have any public constructors.
What you can do is use the existing PLINQ infrastructure to do what you want. All you really are wanting to do is do a Where
with a Contains
being the predicate... So do that.
public static ParallelQuery<TSource> WhereContains<TSource, TKey>(
this ParallelQuery<TSource> source,
IEnumerable<TKey> values,
Func<TSource, TKey> keySelector)
{
HashSet<TKey> elements = new HashSet<TKey>(values);
return source.Where(item => elements.Contains(keySelector(item)));
}
This performs the Where
clause in parallel, and (while not documented) Contains
is thread safe as long as you are not performing any write operations, and because you are making a local HashSet to perform the lookup you don't need to worry about writes happening.
Here is a example project that prints out to the console what thread and item it is processing, you can see that it is using multiple threads.
class Program
{
static void Main(string[] args)
{
List<int> items = new List<int>(Enumerable.Range(0,100));
int[] values = {5, 12, 25, 17, 0};
Console.WriteLine("thread: {0}", Environment.CurrentManagedThreadId);
var result = items.AsParallel().WhereContains(values, x=>x).ToList();
Console.WriteLine("Done");
Console.ReadLine();
}
}
static class Extensions
{
public static ParallelQuery<TSource> WhereContains<TSource, TKey>(
this ParallelQuery<TSource> source,
IEnumerable<TKey> values,
Func<TSource, TKey> keySelector)
{
HashSet<TKey> elements = new HashSet<TKey>(values);
return source.Where(item =>
{
Console.WriteLine("item:{0} thread: {1}", item, Environment.CurrentManagedThreadId);
return elements.Contains(keySelector(item));
});
}
}
score:1
Could you not just do this?
public static ParallelQuery<TSource> Where<TSource>(
this ParallelQuery<TSource> source,
Func<TSource, bool> predicate)
{
return
source
.SelectMany(x =>
predicate(x)
? new TSource[] { x }
: Enumerable.Empty<TSource>());
}
Source: stackoverflow.com
Related Articles
- How to dynamically add OR operator to WHERE clause in LINQ
- LINQ query to perform a projection, skipping or wrapping exceptions where source throws on IEnumerable.GetNext()
- linq styling, chaining where clause vs and operator
- C# Dynamic Linq: Implement "Like" in The Where Clause
- LINQ WHERE method alters source collection
- What's the difference between multiple where clauses and && operator in LINQ-to-SQL?
- Ternary operator in LINQ where clause
- Where can I view LINQ source code?
- LINQ Source Code Available
- Use == operator with generic type in a Where Linq statement
- Implement in operator in Entity Framework
- Linq with where clause in many-to-many EF Code First object
- .NET 4 Code Contracts: "requires unproven: source != null"
- Where with IN operator using Linq on NHibernate
- How to query by where clause with EF code first
- C# - Linq optimize code with List and Where clause
- Ternary Operator in LINQ Where clause for nullable bool column
- no improvements on the following PLINQ code
- creating Linq to sqlite dbml from DbLinq source code
- Where Clause in LINQ with IN operator in Vb
- Binding source thread in PLINQ
- Linq: query syntax where operator does not understand predicates of type Expression
- How to use IN Operator inside Where Condition in Linq
- Concatenate where queries with or operator in a QueryBuilder
- Where is the source for System.Linq.Enumerable.ToList()?
- How to use OR operator in LINQ WHERE statement
- Where is the code for the EDML generated Models?
- Where can I get information on the ! operator used by VB.Net in Linq to DataSet?
- C# Linq use Where with IsNull() Operator '&&' cannot be applied to operands of type 'bool' and 'int?'
- EF Code First: Foreign Key With Where Clause?
- LINQ - Group quarterly records
- Insert Anonymous object in a list of same anonymous type
- How to use whereif in LINQ
- Extracting only certain indexes
- using split in linq query
- Linq to SQL: .FirstOrDefault() not applicable to select new { ... }
- Dynamic Pivot Linq C#
- Linq: check if one of the property in a list is null for all the objects
- Error 6046: Unable to generate function import return type of the store function
- How to get summary of Cities from States
- date format not correct when pulling from database using linq query
- Getting specific columns from an INCLUDE statement with EF
- Lambda expression using Join for something more than a simple key
- Linq Querying On Collection attributes and Matching all Children
- asp.net mvc adding list to viewmodel
- WPF and MusicPlayer
- MVC3 Razor @Html.DropDownListFor
- LINQ - JSON Help needed
- linq query for selecting records which have first rank
- How to apply ordered values to the same BindingList?