Hello, I have a small problem with assigning property values from one list of elements to another. I know that I can solve this problem in the "old way", iterate through both lists, etc., but I'm looking for a more elegant solution using LINQ.
Let's start with the code ...
class SourceType { public int Id; public string Name; // other properties } class DestinationType { public int Id; public string Name; // other properties } List<SourceType> sourceList = new List<SourceType>(); sourceList.Add(new SourceType { Id = 1, Name = "1111" }); sourceList.Add(new SourceType { Id = 2, Name = "2222" }); sourceList.Add(new SourceType { Id = 3, Name = "3333" }); sourceList.Add(new SourceType { Id = 5, Name = "5555" }); List<DestinationType> destinationList = new List<DestinationType>(); destinationList.Add(new DestinationType { Id = 1, Name = null }); destinationList.Add(new DestinationType { Id = 2, Name = null }); destinationList.Add(new DestinationType { Id = 3, Name = null }); destinationList.Add(new DestinationType { Id = 4, Name = null });
I would like to do the following:
- destinationList must be filled with the names of the corresponding records (by identifier) in sourceList
- destinationList should not contain entries that are not present in both lists at the same time (for example, Id: 4,5 should be excluded) - something like an internal join
- I would like to avoid creating a new mailing list with updated entries, because both lists already exist and are very large, so there is no “convert" or "choose new".
At the end, destinationList should contain:
1 "1111" 2 "2222" 3 "3333"
Is there any elegant (single line Lambda ?;) solution for this with LINQ?
Any help would be greatly appreciated! Thanks!
source share