score:0
one option is to do an union
operation, by specifying an equalitycomparer
. if the order is important, you can do an orderby
operation at the end.
class textidcomparer : equalitycomparer<text> {
public override bool equals(text x, text y) => x.id == y.id;
}
var result = listb.union(lista, new textidcomparer()).orderby(x => x.id)
score:2
it might be more efficient to convert listb
to a dictionary first:
var dictb = listb.todictionary(x=> x.id)
then you can write
var result = lista.select(x => dictb.trygetvalue(x.id, out var b) ? b : x)
upd rewrote taking comment suggestions into account
score:3
you can try looking for id
within listb
:
var result = lista
.select(a => listb.firstordefault(b => b.id == a.id) ?? a);
here for each a
within lista
we try to find corresponding by id
(b.id == a.id
) item within listb
. if no such item is found we just return lista
item: ?? item
in case of .net 6 you can use overloaded .firstordefault
version (we can pass a
as a default value):
var result = lista
.select(a => listb.firstordefault(b => a.id == b.id, a));
Source: stackoverflow.com
Related Query
- Select all from one List, replace values that exist on another List
- LINQ Lambda - Find all ID's in one list that don't exist in another list
- Linq select all numbers that are in any interval from another list of intervals
- How to filter linq query based on all of the values of one property from another list
- How do I select all objects from a list that have no related objects in another list/model
- I need all values in one list that "connect" to all values in another list
- Need to get all records from one table, that match values I return from another table
- Linq query, select everything from one lists property that starts with a string in another list
- LINQ - Find all items in one list that aren't 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?
- How can I set properties on all items from a linq query with values from another object that is also pulled from a query?
- Assign values from one list to another using LINQ
- Check if one list contains all items from another list in order
- Using Linq, get all items from list that are in another List<int>
- Select All object values to list from dictionary
- How to select objects from a list that has a property that matches an item in another list?
- select multiple objects from list based on values from another list
- Using LINQ I have a list of lists, how do I select all objects that exist in every list?
- Find all objects in one list that have a property that matches a property in another list of objects
- Assign Values from one list to Another list based on Id
- Excluding items from a list that exist in another list
- LINQ select from table where fieldValue is in another tables list of values
- Linq for selecting elements from list 1 that exist on list 2 by comparsion between 2 properties values
- How to effectively select all values matching given pattern from a list of struct?
- How do I get a list of integers that exist in one collection, but not in another of a different type using LINQ?
- Linq one list to select multiple items from another list
- removing items from list that exist in another list based on an ID member
- Select values from one table based on specific value of another table Linq
- Using linq to select all values from one column based on a distinct second column
More Query from same tag
- IQueryable<T>.Include() gets ignored
- Linq to Entity - can i get to another table in the model when in the list template MVC page
- Create batches in linq
- Complicated Linq filtering without nesting countless if/foreach
- how to merge duplicate records into one record by linq
- vb.net datatable asenumerble get distinct vlues and count grouped duplicate rows
- c# group list into new list
- More efficient method for querying twice-separated-or-more objects in a relation chain?
- Speeding up a linq query with 40,000 rows
- LINQ - Select Specific Columns
- Trouble mapping to DTOs with LINQ
- Any way to make this LINQ faster?
- Cannot implicitly convert type 'System.Linq.IQueryable<AnonymousType#6>' to 'decimal'
- LINQ join with multiple conditions?
- C# LINQ: Get items with max price
- Projecting an IEnumerable inside an Projected IQueryable making N Requests to the Database
- Weird extention method behaviour in LINQ query
- How to put the U in F# Type Provider CRUD?
- LINQ how to handle null values
- find duplicate objects within a list of lists (using linq?)
- How to get moving combination from two List<String> in C#?
- Linq-to-NHibernate OrderBy Not Working
- Use C# Expression Evaluator for filtering a list
- Does IEnumerable<TSource> Concat<TSource> preserve the order of elements?
- Group objects into fixed-sized collections
- C# Linq Query on database using field name as a variable
- How can I use LINQ to find a DataGridView row?
- linq select sum and average into view model
- Using Math functions in Entity framework with mysql
- Linq To Objects, List<Items>, where Items consist of List<Items2>