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 Query
- LINQ to Object - How to implement WHERE clause `If at least one of the elements is` for sub-group
- How to implement SQL where in for entity framework 6
- How to dynamically add OR operator to WHERE clause in LINQ
- How to use LINQ Where for generic type?
- LINQ How to define a default type for use with ElementAtOrDefault Operator
- How to reuse a linq expression for 'Where' when using multiple source tables
- Linq : How do I test a List<bool> for condition where any one of its values == true?
- linq - how do you do a query for items in one query source that are not in another one?
- LINQ for CRM how to use C# list in where clause
- How can I get rid of having to prefix a WHERE query with 'N' for Unicode strings?
- How to re-implement a Where() operator for PLINQ?
- How to query by where clause with EF code first
- Ternary Operator in LINQ Where clause for nullable bool column
- How to Implement GetEnumerator method for class that implements IEnumerable<IEnumerable<T>>
- How to create regex for mathing linq statement NOT contains where clause?
- How to use IN Operator inside Where Condition in Linq
- Where is the source for System.Linq.Enumerable.ToList()?
- How to implement nested for loop into Linq?
- How to use OR operator in LINQ WHERE statement
- Where is the code for the EDML generated Models?
- How to use PLINQ for IO-bound tasks?
- How can I refactor this code for LINQ filtering?
- How can I check for null values in this linq query where clause
- How to code a Foreach Loop for Endless Categories Tree in MVC.Net?
- With a generic repository for entity framework, how do I query entities which implement a specific interface?
- How do I overcome an "Unable to create a constant value of type ..." exception for a simple WHERE statement
- How to assign multiple LINQ Include() statements to a variable for code re-use?
- How to implement First and FirstOrDefault for boolinq?
- How to get PEX to automatically generate inputs for code involving LINQ
- How would you implement a general case for this property mapping to a serializable class object?
More Query from same tag
- Convert XElement to string
- Linq query using group by and having
- Join two tables using linq, and fill a Dictionary of them
- LINQ Query against Azure table service fails
- LINQ query AND & OR
- Cannot implicitly convert type System.Collection.Generic.IEnumberable
- C# LINQ Join Error
- Entity framework nested query
- LINQ - exclude the filter when the value is NULL
- LINQ to Entities: Get more recent date from each group
- Iteratin trought LINQ with index in collection
- Linq + MoreLinq how to aggregate one result prop to list?
- Get count of a datatable column contains with specific value
- Filter ICollection of objects where any criteria matches the ones from a list of ints usin Linq
- Access child token in JSON
- Create an object collection based on another collection with different type
- Get Longest Name
- Read XML node from certain element forward?
- linq Command not getting the answer for DB
- Avoid "Parameterless Queries Error" with Linq Let clause
- Select all columns after JOIN in LINQ
- Hierarchical Update
- Applying Group By in LINQ
- Relax C# LINQ String Comparison (Trim, Case Insensitive, ??)
- merging 2 collections and find all the unique items
- Query CSV using LINQ C# 2010
- Call Length Property on Returned Array in Chained String/LINQ Methods of C#
- How to loop values in table valued function in C# though Entity framework?
- Joining tables in EF Core Linq Query
- what is the most elegant way to update an item in one list from another list in C#?