score:1
the best i can think of is :
a) create dictionary and use its fast lookups
b) use linq .where
method with .containskey()
on this dictionary which internally uses hashtable
and performs quick lookups.
this should reduce search complexity to almost o(1)
rather than o(n)
ro worse (when we use linq .where()
with .any()
or .contains()
and that leads to nested loops).
from msdn page :
the dictionary generic class provides a mapping from a set of keys to a set of values. each addition to the dictionary consists of a value and its associated key. retrieving a value by using its key is very fast, close to o(1), because the dictionary class is implemented as a hash table.
so what we can do is :
dictionary<string, string> dict = listproviderkeys.todictionary(s => s);
var newlist = secondlist.where(e => !dict.containskey(e.providerkey)).tolist();
here is a very simple, short, but complete example illustrating it and also testing its performance :
class person
{
public int id { get; set; }
}
class program
{
static void main(string[] args)
{
list<int> ints = new list<int>();
list<person> people = new list<person>(1000);
for (int i = 0; i < 7500; i++)
{
ints.add(i);
ints.add(15000 - i - 1);
}
for (int i = 0; i < 45000; i++)
people.add(new person() { id = i });
stopwatch s = new stopwatch();
s.start();
// code a (feel free to uncomment it)
//dictionary<int, int> dict = ints.todictionary(p => p);
//list<person> newlist = people.where(p => !dict.containskey(p.id)).tolist();
// code b
list<person> newlist = people.where(p => !ints.contains(p.id)).tolist();
s.stop();
console.writeline(s.elapsedmilliseconds);
console.writeline("number of elements " + newlist.count);
console.readkey();
}
on release mode results are :
both code a & code b outputs 30 000 elements but :
it took more than 2000 ms with code b and only 5 ms with code a
score:-2
i believe this will work:
list<changessummary> exceptionlist = secondlist.
where(x => !listproviderkeys.any(key => x.providerkey == key)).tolist();
score:-1
i think in this case simple where
would be easier and more readable to apply.
var first = new list<string> { "a" };
var second = new list<changessummary>()
{
new changessummary() { providerkey = "a" },
new changessummary() { providerkey = "b" }
};
var result = second.where(item => !first.contains(item.providerkey));
// result
// .tolist()
// .foreach(item => console.writeline(item.providerkey));
score:0
public class programm
{
public static void main()
{
list<changessummary> summaries = new list<changessummary>();
summaries.add(new changessummary()
{
fieldname = "1",
providerkey = "test1",
});
summaries.add(new changessummary()
{
fieldname = "2",
providerkey = "test2",
});
summaries.add(new changessummary()
{
fieldname = "3",
providerkey = "test3",
});
list<string> listproviderkeys = new list<string>();
listproviderkeys.add("test1");
listproviderkeys.add("test3");
var res = summaries.where(x => !listproviderkeys.contains(x.providerkey));
res.tolist().foreach(x => console.writeline(x.providerkey));
console.readline();
}
}
public class changessummary
{
public string tablename { get; set; }
public string providerkey { get; set; }
public string provideradrskey { get; set; }
public string providerspecialtykey { get; set; }
public string fieldname { get; set; }
}
Source: stackoverflow.com
Related Query
- Excluding items from a list that exist in another list
- removing items from list that exist in another list based on an ID member
- Find items from a list which exist in another list
- Exclude list items that contain values from another list
- Linq - How to select items from a list that contains only items of another list?
- Using Linq, get all items from list that are in another List<int>
- Get items from input list that do not exist in the database using EF Core 2.1
- Select all from one List, replace values that exist on another List
- Using LINQ to remove items from a list that do not apear in another list
- Remove items from a List that are in another List by specific property with grouping
- Get the items from a list that match all itens from another list
- LINQ - Find all items in one list that aren't in another list
- Remove items of list from another lists with criteria
- LINQ return items in a List that matches any Names (string) in another list
- Linq selecting items that exist in both list
- How do I move items from a list to another list in C#?
- Remove items from list that intersect on property using Linq
- LINQ Lambda - Find all ID's in one list that don't exist in another list
- How can I set properties on all items from a linq query with values from another object that is also pulled from a query?
- Check if one list contains all items from another list in order
- How to generate a unique list of items from another list using LINQ in C#
- Adding only new items from one list to another
- Linq - Excluding items from different list types
- Select items from a List where the children contain the items from another List using LINQ
- linq - how do you do a query for items in one query source that are not in another one?
- How to use LINQ to query list of strings that do not contain substring entries from another list
- Selecting items from generic list based on true condition from another list
- C#- Select specific items from a list based on partial intersection with another list (Linq + Lambda)
- How to select objects from a list that has a property that matches an item in another list?
- Using LINQ to get a list of items where the item contains a part of an item from another list
More Query from same tag
- change order on my object list
- LINQ support in Enterprise Library Data Access Application Block
- Fun things to do with generators and sequences
- Union two models together causes "The specified LINQ expression contains references to queries that are associated with different contexts."
- How to filter a collection of a collection in linq?
- Mapping POCO to string in Linq to Sql so that I can use Linq on my POCO
- Selecting top two of the combined group by linq
- EF DbContext on a generic repository does not perform the expected includes when transforming the data into a list
- How to OrderBy by related entity in EntityFramework
- c# How can I create a collection of custom objects without going through each field
- Is LINQ Where extension method optimized for SortedDictionary?
- Join two tables issue in linq
- How to bind LINQ result to datagrid
- LINQ Query - exclude the values of one list from another list
- How does Distinct() work on object list?
- Compiler can't choose between generic extension methods with different where conditions
- Concatenating Two Different IEnumerable Objects in C#
- How to perform sql join on Dictionary<string,List<string>>?
- Tricky Linq Group by for time ranges
- How to display Model object properties in razor view VB.net
- IQueryable to List<T>
- How to desearilize Json and bind to variables/properties c#
- How to compare two string arrays and store the matching values in another string array?
- Unable to compare two dates in LINQ
- Convert Expression<Func<IBar, T>> to Expression<Func<Bar, T>>
- How does adding a record in db using Linq to SQL work?
- Left Join on Linq query when also using the Where clause on joint table
- Linq to Sql, group by 2 property and substring
- LINQ way to get items between two indexes in a List
- How can I Using LinQ search in a list by the input and print those that contain the letters?