NET, I need to copy the values of an object item in one list into another list, this is what i have:
List<Business> businesses; // from where I want to copy the value
PaginationResult<Business> paginatedBusinesses // contains the final list
The type paginated result has a list inside it:
public class PaginationResult<T>
{
public PaginationResult();
//
// Summary:
// This is the collection of paged items
public List<T> Items { get; set; }
The Business object contains these attributes:
public class Business : IAggregateRoot
{
/// <summary>
/// Unique identifier of the business
/// </summary>
public Guid Id { get; set; }
...
/// <summary>
/// The epoch timestamp this user last accessed this business.
/// </summary>
public long? LastAccessed { get; set; }
Here i converted the items of the paginatedBusiness list into the Business class:
var paginatedBusinesses = results.ConvertItems<Business>((i) => i.ToDomainEntity());
I need to copy the LastAccessed values from the businesses list into paginatedBusinesses.Items. searching for the Id (both lists have the id with the same values but in the first we have the LastAccessed value and in the final list we don't have it). How can I do it? I tried with something like:
paginatedBusinesses.Items.AddRange(businesses.Select(i => i.LastAccessed));
But it does not work, how can i put it in the object that correspond (the one that have the same id), also i tried with a foreach but not sure how to do it. Any ideas? Thanks!
CodePudding user response:
If I understand correctly: both the pagenatedBusinesses.Items
and the businesses
lists contain Business
objects with the same Ids.
The question: How do I copy the LastAccessed
values from items in the businesses
list to items in the pagenatedBusinesses.Items
list that have the same Id
?
If that is correct then the following code will copy the LastAccessed
value from the Business
objects in the businesses
list into the Business
objects with the same Id
in the pagenatedBusinesses.Items
list:
paginatedBusinesses.Items
.Join(
businesses,
p => p.Id, b => b.Id,
(p, b) => new {PagenatedBusiness = p, b.LastAccessed})
.ToList()
.ForEach(i => i.PagenatedBusiness.LastAccessed = i.LastAccessed);