score:1
Now that this has been clarified, the question is basically this:
- Combine all elements from list A and list B, and if there are any duplicates, keep the one with the newest
LastGamePlayed
.
I would do this with a grouping:
var players = from p in listA.Concat(listB)
group p by p.ID into g
select g.OrderByDescending(x => x.LastGamePlayed).First();
If performance is an issue, there are "faster" ways to write this, but I would start with this.
score:2
You should be able to use the linq enumerable extension Concat and Exept to achieve this.
listA.Concat(listB.Except(listA);
This will remove items in B that match A, and them add the result to A.
You will have to write an IEqualityComparer which compares by ID.
Documentation for theses methods can be found here:
IEqualityComparer documentation can be found here:
score:0
You can write an O(n log n) solution by building a set from list2 then overwriting (or inserting) items with list1, then converting that set back to a list.
If the lists are already sorted, you could do it O(n) by merging by hand.
Source: stackoverflow.com
Related Articles
- Combining lists but getting unique members
- Getting distinct and ordered members from a list of strings - linq or hashset for unique which one is faster / better suited
- Combining a list of objects with periods into unique lists
- LINQ Partition List into Lists of 8 members
- Combining Lists in Lambda/LINQ
- Getting unique values from a list of objects with a List<string> as a property
- LINQ Getting Unique items from a List within a List
- Combining 2 lists and and remove duplicates .Output in a third list .My attempts do not work
- LINQ Source Code Available
- Combining child class lists to parent class list
- C# Code Contracts -- How to ensure that a collection of items contains items with unique properties?
- .NET 4 Code Contracts: "requires unproven: source != null"
- Getting the difference between two Lists based on certain fields
- EF code first - getting DynamicProxies instead of objects. Why?
- How to join a list of lists in a unique list using Linq
- Divide a single List of Class Objects into multiple Lists based on Class Members C#
- Get all members that do not match between 2 lists
- creating Linq to sqlite dbml from DbLinq source code
- Combining two datatable and getting the amount from second table by using linq
- Combining lists in linq
- Getting the Total Number of Elements From All Lists Belonging to An Object
- Merging 2 lists with linq based on unique Id to avoid duplicates
- Getting unique values using Linq to Entities
- C# - Getting flat many-to-many List of Objects into List of distinct combinations of two Lists of objects
- Getting a list of unique domains from email list
- Combining information from multiple lists
- Getting 'Data source is an invalid type' when binding Linq query to Gridview
- I'm trying to convert a SQL query to linq, but I'm getting stuck on combining group by and max()
- Getting "Could not find an implementation of the query pattern for source type 'ExcelQueryable<T>'. " Error
- How to merge two lists while adding metadata indicating value source with LINQ statement?
- Linq. Anonymous type error when joining to multiple tables
- Domain logic vs data validation
- Is it Possible to use reflection on LINQ to Entities to query a dynamic table?
- Retrieve All Children From Parent[]
- Get the name of a JObject in Json.Net
- How to declare var datatype in public scope in c#?
- How to Select new model list after GroupBy mothod in Linq?
- compound linq query
- Nested for loops - RadioButtonFor not working
- LINQ query JOIN two tables for Web API controller method
- linq lambda convert object with list of objects of the same type to another object
- Why does "linq to sql classes" change the name of a table when making a class?
- Unwanted Change of Input in Func c#
- Grouping troubles
- Get Max Value in Child Table with LINQ lambda
- linq latests elements that match a criteria
- Linq left join with multiple columns and default values
- C# 'Newtonsoft.Json.Linq.JValue' does not contain a definition for 'Contains'
- Combining consecutive dates into ranges
- Nested .Select() inside of .Where()