score:2
Accepted answer
so the first case is your basic join
operation, which linq supports:
var matchingitems = from item1 in list1
join item2 in list2
on new{ item1.id, item1.categoryid}
equals new{ item2.id, item2.categoryid}
select new { item1, item2 };
for the second case:
var idvalues = list2.select(item => new { item.id, item.categoryid })
.tohashset();
var missingitems = list1.where(item => !idvalues.contains(new { item.id, item.categoryid }));
here is a definition of tohashset
:
public static class extensions
{
public static hashset<t> tohashset<t>(this ienumerable<t> source)
{
return new hashset<t>(source);
}
}
score:2
you can also use a join
clause, although it would require you to initialize a number of anonymous types.
var result = (from l1 in list1
join l2 in list2 on new {l1.id, l1.category}
equals new {l2.id, l2.category}
select l1).toarray();
score:3
you can implement a simple icomparer
for your type of data and use
var intersection = list1.intersect(list2, mycategoryidcomparer);
var notrepeating = list1.union(list2).except(intersection);
var notinlist1 = list2.except(list1, mycategoryidcomparer);
you can implement the icomparer
easily:
public class categorycomparer : icomparer {
int icomparerlist1.compareintersect( object x, object y ) {
// do some type checks to make sure both are categoryobject
return ((categoryobject)x).categoryid
.compare(((categoryobject)y).categoryidmycategoryidcomparer);
}
}
Source: stackoverflow.com
Related Query
- Linq for selecting elements from list 1 that exist on list 2 by comparsion between 2 properties values
- LINQ for removing elements that are started with other element from list
- Linq selecting items that exist in both list
- Linq to entities to return a list that contains elements from another list
- Construct a list of wpf Hyperlink elements from an XML source file using Linq
- Turning a chain of foreach's for inserting elements from a list into another list into linq
- c# Linq or code to extract groups from a single list of source data
- use linq to get a sub list that contains elements from an array
- 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
- Linq - How to select items from a list that contains only items of another list?
- c# linq - get elements from array which do not exist in a different array
- How to understand the following C# linq code of implementing the algorithm to return all combinations of k elements from n
- How can I add elements from one collection that do not exist in a second collection to a third collection?
- Using Linq query inside List<T>.AddRange to conditionally add elements from one list to another
- Why can't I change elements from a linq IEnumerable in a for loop?
- Get groups of 4 elements from name value list using LINQ in C#
- Getting distinct and ordered members from a list of strings - linq or hashset for unique which one is faster / better suited
- How do I use Linq to find the elements of a list that are not present in another list?
- linq - how do you do a query for items in one query source that are not in another one?
- Selecting elements from XML file using LINQ
- How to use LINQ to query list of strings that do not contain substring entries from another list
- C# LINQ selecting a list with a property that has multiple value
- Create a List of elements from a DataTable LINQ Column
- Get the count of distinct elements from a list of lists using LINQ
- LINQ in C#. Check if one list contains elements from another one
- Using LINQ I have a list of lists, how do I select all objects that exist in every list?
- Selecting "custom distinct" items from a List using LINQ
- Selecting similar elements from same list
- Linq query for selecting an item from tree structure, but to look in the whole depth
More Query from same tag
- Null value in linq where clause
- Using Enumerable.Contains method for null item checking
- Prevent 'NOW()' in LINQ to EF query
- Linq Lambda Where clause with in a where clause
- How to generate elements of IEnumerable One by One
- .NET LINQ2XML parsing XML and getting all nodes with specifc name
- Linq to Entities - how to delete many records with inner join
- Access Identity User and Role Tables in Linq
- How to Get Set Linq Entity With Only String Name
- Get root descendant element using value of another element
- How to do multiple left outer joins in a Linq Query?
- Linq - Excluding users from list in IQueryable expression - What is the correct way?
- Search IList of KeyValuePairs for two keys
- Compare if string that user inputs exists in list using C#
- Convert LINQ to XML
- Cant use Linq with nested class List<> on MongoDb C#
- How to properly create a union from two tables selecting only specifc columns where one column is going to empty with some text
- why are there 2 different variables in this linq statement?
- Why Linq Except() with custom Equals IEquatable return items when he shouln't
- Handling auto-incrementing IDENTITY SQL Server fields with LINQ to SQL in C#
- Compare List of Integers and add/remove the rows in database using the difference in result LinqtoSQL
- Create String Array from database
- Query Entire Junction Table
- Unable to Find out Foreign Key related values without duplication
- LINQ - GROUP BY IEnumerable
- How to update an existing item in SQL database using linq?
- Filter data from DataTable
- Filter Linq Child Collection by another List/Array
- Dynamically incremented property for IEnumerable collection?
- How to add attributes to an element using LINQ, C#?