score:1
Simple SelectMany
should be enough to flatten the collection:
customer.ContentTypePreferences = source.RequestCustomer.MarketingPreferences
.SelectMany(mp => mp.ContentTypePreferences)
.Select(contentPrefs => new ContentTypePreferences
{
TypeId = contentPrefs.Type,
OptIn = contentPrefs.OptIn,
ValidFromDate = contentPrefs.ValidFromDate
})
.ToList();
score:1
I did find a way to solve this issue using the below.
customer.MarketingPreferences = source.RequestCustomer.MarketingPreferences
.Select(x => new MarketingPreferences()
{
ChannelId = x.Channel,
OptIn = x.OptIn,
ValidFromDate = x.ValidFromDate,
UpdatedBy = Functions.DbUser,
CreatedDate = DateTime.UtcNow,
UpdatedDate = DateTime.UtcNow,
ContentTypePreferences = (from c in x.ContentTypePreferences
select new ContentTypePreferences
{
TypeId = c.Type,
OptIn = c.OptIn,
ValidFromDate = c.ValidFromDate,
ChannelId = x.Channel // Should inherit parent marketing preference channel
}).ToList(),
UpdatingStore = null // Not passed by API
})
.ToList();
I appreciate the other answers, but realised that just flattening might not be enough, as the ChannelId for each ContentTypePreference isn't passed in with the POST JSON but is instead derived from the parent MarketingTypePreference for that ContentTypePreference (e.g. a child of EMAIL will have a channel Id of EMAIL) so the above solution lets me inject that when creating each new ContentTypePreferences for Entity Framework.
Source: stackoverflow.com
Related Articles
- c# using Linq to project an array within an array onto a new array?
- Using LINQ to project a list to a key / count 2-dimensional array
- Linq code to get the index of an object in an array from an object within a list
- How to reinsert data from one table onto itself using LINQ in code migration?
- Selecting Distinct Items within Array using PowerShell and Linq
- Convert string[] to int[] in one line of code using LINQ
- How do I find the text within a div in the source of a web page using C#
- Convert string to int array using LINQ
- How to select values within a provided index range from a List using LINQ
- Mapping a list of object models onto another list using linq
- using LINQ to find the cumulative sum of an array of numbers in C#
- Take the first five elements and the last five elements from an array by one query using LINQ
- Using LINQ to split items within a list
- Combining array of arrays into single, distinct array using LINQ
- Is there a neat way of doing a ToList within a LINQ query using query syntax?
- Obtaining the min and max of a two-dimensional array using LINQ
- How to get a byte array length using LINQ to Entities?
- Random array using LINQ and C#
- Splitting an array using LINQ
- Using LINQ to search a byte array for all subarrays that start/stop with certain byte
- Array operations with n-dimensional array using LINQ (C#)
- Using C# Linq to return first index of null/empty occurrence in an array
- Using LINQ To Query Int Ids From An Array
- Any better way for converting array to concurrent dictionary using linq or IEnumerable?
- C# Using Linq to get column from jagged array
- Multiply a native C# array by a factor using Linq
- Change one field in an array using linq
- Convert string array to custom object list using linq
- Retrieving Data from database within the last 7 days using linq
- Linq - Using array in Lambda expression to fetch multiple records
- c# join two int lists where values are the same and values are at same index
- How can I obtain objects from list with C# LINQ using boolean conditions?
- How to automatically fill xml ? linq to xml
- LINQ to Object Basic
- linq xml all inner nodes
- EF tables with Many to many relationship, Will this consume memory?
- Cannot build the Test project for LINQ IQueryable Toolkit (IQToolkit) - Code 9009
- Grouping and SUM with LINQ (on conditions)
- Get date range after date selection
- Convert a delimited string to a dictionary<string,uint> in C#
- Stop linq from caching tables
- Create a database from a DataContext with integrated security
- LINQ to SQL two contexts
- Group by two parameters and count in linq, considering occurrences of 0
- IEnumerable.GroupJoin and Entity Framework objects
- convert string in an array of objects
- c# build hierarchy in reverse
- Pairing Collections in Linq without a For Loop
- Can LINQ support this simple algorithm?
- Cosmos Db linq query for child item not working