score:0
If you have a union and intersection of the form:
var union = source.Where(predicate0).Union(source.Where(predicate1));
var inter = source.Where(predicate0).Intersect(source.Where(predicate1));
Then union
will have the values for which either predicate0
or predicate1
was true while inter
will have the values for which both predicate0
and predicate1
were true.
For this reason, the following would have the same result:
var union = source.Where(predicate0.Or(predicate1));
var inter = source.Where(predicate0.And(predicate1));
This depends upon the components of the union and intersection being produced by two Where
queries though. In the likes of:
var union = source1.Where(predicate0).Select(item => item.ID)
.Union(source2.Where(predicate1).Select(item => item.ID));
Then it's likely that the predicates aren't even of the same type, and there are yet other cases where combining predicates will not work as a replacement of Union
and Intersect
.
It is though useful to be able to consider the first examples both ways, in terms of one's understanding of how predicates, unions and intersections work.
Source: stackoverflow.com
Related Articles
- How to reuse a linq expression for 'Where' when using multiple source tables
- LINQ Source Code Available
- How to use Expression for Linq union and intersect?
- creating Linq to sqlite dbml from DbLinq source code
- Linq to Sql: Optimizing lamba expression - clean code
- EF Code First - Linq to Entities Union EqualityComparer
- Linq union all equivalent of sql code
- C# HashSet union on IEnumerable within LINQ Func expression does not work (possible precompiler bug)
- Unreachable expression code in LINQ query
- source code for LINQ 101 samples
- Linq Expression Refactor Duplicate Code
- Intersect or union with a custom IEqualityComparer using Linq
- optimise code into linq expression
- A LINQ question: map query expression to c# code
- Convert this linq code to expression
- Union two models together causes "The specified LINQ expression contains references to queries that are associated with different contexts."
- how to translate a linq expression into sql string use c# code
- LINQ Intersect Query for Many to Many relationship using .NET4 MVC4 Code First
- c# Linq or code to extract groups from a single list of source data
- Saved projection expression for re-use in different linq expressions with two source objects
- Some help on a Union LINQ expression please
- EF5 code first datetime2, building a linq expression
- Convert string[] to int[] in one line of code using LINQ
- Code equivalent to the 'let' keyword in chained LINQ extension method calls
- String.IsNullOrWhiteSpace in LINQ Expression
- How to flatten nested objects with linq expression
- LINQ to Entities does not recognize the method 'System.String ToString()' method, and this method cannot be translated into a store expression
- Linq code to select one item
- "Or" equivalent in Linq Where() lambda expression
- Union Vs Concat in Linq
- Cast to string won't let me use All() method
- C# Merge properties from two (or more) lists based on common property
- How does linq Expression<TDelegate> assignment work on a language syntax level
- InvalidOperationException when using select query in ASP.NET Core MVC
- Convert Sum(case) SQL statement to LINQ query in Entity Framework
- Linq Group & SUM
- Multiple on clause from DataTable to DataTable via LINQ
- Entity framework - Linq GroupBy data from multible tables
- How to simultaneously sort 2 lists using LINQ?
- C# Linq where clause as a variable
- "Or" equivalent in Linq Where() lambda expression
- Why are Where and Select outperforming just Select?
- How to implement generic method approach using Linq and XML
- Using Linq to Query XML Document
- How do I write a linq to sql query to find records where field name can match one of dynamic number of strings?
- Find missing required values in a List
- How to Retrieve XML using Linq Lambda?
- Can't figure out how to use Linq to XML to do conditional adding of Nodes?
- Joining two tables and returning multiple records as one row using LINQ
- using string as a condition in LINQ