score:3
darren kopp's answer:
var excluded = items1.except(items2);
is the best solution from a performance perspective.
(nb: this true for at least regular linq, perhaps linq to sql changes things as per marco russo's blog post. however, i'd imagine that in the "worst case" darren kopp's method will return at least the speed of russo's method even in a linq to sql environment).
as a quick example try this in linqpad:
void main()
{
random rand = new random();
int n = 100000;
var randomseq = enumerable.repeat(0, n).select(i => rand.next());
var randomfilter = enumerable.repeat(0, n).select(i => rand.next());
/* method 1: bramha ghosh's/marco russo's method */
(from el1 in randomseq where !(from el2 in randomfilter select el2).contains(el1) select el1).dump("result");
/* method 2: darren kopp's method */
randomseq.except(randomfilter).dump("result");
}
try commenting one of the two methods out at a time and try out the performance for different values of n.
my experience (on my core 2 duo laptop) seems to suggest:
n = 100. method 1 takes about 0.05 seconds, method 2 takes about 0.05 seconds
n = 1,000. method 1 takes about 0.6 seconds, method 2 takes about 0.4 seconds
n = 10,000. method 1 takes about 2.5 seconds, method 2 takes about 0.425 seconds
n = 100,000. method 1 takes about 20 seconds, method 2 takes about 0.45 seconds
n = 1,000,000. method 1 takes about 3 minutes 25 seconds, method 2 takes about 1.3 seconds
method 2 (darren kopp's answer) is clearly faster.
the speed decrease for method 2 for larger n is most likely due to the creation of the random data (feel free to put in a datetime diff to confirm this) whereas method 1 clearly has algorithmic complexity issues (and just by looking you can see it is at least o(n^2) as for each number in the first collection it is comparing against the entire second collection).
conclusion: use darren kopp's answer of linq's 'except' method
score:0
here's a more simple version of the same thing, you don't need to nest the query:
list<string> items1 = new list<string>();
items1.add("cake");
items1.add("cookie");
items1.add("pizza");
list<string> items2 = new list<string>();
items2.add("pasta");
items2.add("pizza");
var results = from item in items1
where items2.contains(item)
select item;
foreach (var item in results)
console.writeline(item); //prints 'pizza'
score:1
another totally different way of looking at it would be to pass a lambda expression (condition for populating the second collection) as a predicate to the first collection.
i know this is not the exact answer to the question. i think other users already gave the correct answer.
score:4
from marco russo
northwinddatacontext dc = new northwinddatacontext();
dc.log = console.out;
var query =
from c in dc.customers
where !(from o in dc.orders
select o.customerid)
.contains(c.customerid)
select c;
foreach (var c in query) console.writeline( c );
score:4
use the except extension method.
var items1 = new list<string> { "apple","orange","banana" };
var items2 = new list<string> { "grapes","apple","kiwi" };
var excluded = items1.except(items2);
Source: stackoverflow.com
Related Query
- linq - how do you do a query for items in one query source that are not in another one?
- Use LINQ to get items in one List<>, that are not in another List<>
- Speed Up Performance - LINQ to get items in one LIST, that are not in another List
- How to count write the linq query when the grouped bY column is in one table and the items are in another table
- How do I add a where clause to a field that is not part of the return set for LINQ query
- How are people unit testing code that uses Linq to SQL
- Using LINQ to Objects to find items in one collection that do not match another
- How can I set properties on all items from a linq query with values from another object that is also pulled from a query?
- Using LINQ to find all keys from one collection that are not in another?
- How can I use linq to return integers in one array that do not match up with an integer property of another array?
- Which LINQ query to select rows from 1 table that are not in another table
- Use LINQ to get items in one List<>, that are in another List<>
- How do I use Linq to find the elements of a list that are not present in another list?
- How to use LINQ to query list of strings that do not contain substring entries from another list
- LINQ query for finding one item in list AND verifying list does not contain another item
- How to get items that are and are not in a list
- How do I do a linq query to find fields in a dataset that are present in every record of a set?
- How can I select using LINQ for an entry that contains a LIST with more than one row?
- How to retrieve one table row and list of rows that are connected to the first row in Linq .NET
- How to write Linq (or lambda) statement to produce all element that are NOT IN (or NOT EXIST)
- How do you write a LINQ query that filters a sub table to a specific time period and sums the results of the sub table?
- One to Many LINQ query - Assembly does not have definition for SELECT
- How to declare global variable for linq query that produces a sequence of anonymous types
- How can I code numerous MIN functions into one LINQ to DataSet query
- LINQ query for retrieving items that contain any value from a list?
- c# linq how to check for null values in the same column that i need to be not null
- How can I simplify this LINQ query that searches for keywords in strings and orders them by relevance?
- How to find items in a superset that are not in a subset
- How do I substitute one value for a property in a LINQ query result for another value?
- Convert SQL Server query to LINQ lambda query for two different cols from two different table that are related
More Query from same tag
- Create anonymous object via LINQ lambda from one result
- Make LINQ expression case insensitive
- Find invalid character check against string array in C#
- Check a column with Contain list in EF Core 3.1
- WEB API IQueryable skip() take()
- Getting distinct values from an ArrayList in C#
- Leaking Entity Framework's abstraction - just a little
- Find all possible sequence list from single list c#
- Join with Entity Framework and F#
- LINQ to SQL database row not updating
- Converting SQL query to Linq
- C# - avoid null pointers in LINQ ToDictionary
- C# Linq predicate generation remove all null values from list
- Getting Child nodes of an xDocument element while itereating through the Xdocument
- Construct a LINQ query to return an item from a List that is related by one property to other members but which differs by a second property
- Random Orderby on subquery in LINQ
- Nested Dictionary: group by date and then group by Enum value and summarize the count
- It is possible to get functionality similar to .NET's LINQ in C++?
- Expression parameter is not defined
- CRM2011 - SDK 5.0.3 - Linq to CRM Entities Problem
- Compare 2 dataTable which are linked with primary Key in c# using Linq
- Help converting LINQ expression from C# to VB
- Getting Only The Top Row From Each Group
- How to retrieve parent objects without children
- Getting error: 'System.Collections.Generic.IEnumerable<AnonymousType#1>' does not contain a definition for 'ToList'
- Querying a chain of list of lists with LINQ
- Linq safety checks
- How do I get the sum of the Counts of nested Lists in a Dictionary without using foreach?
- Simple nested repeater example using LINQ in C#
- Related entities not loading when using Include with Linq to Entities