👨‍💻
wiki - owain.codes
  • 💻 Welcome
  • Coding
    • Tips / How-to
      • Where is the hosts file on Windows?
      • Wrapped up a site
    • Snippets
      • Non-coding
        • TinyMCE config generator for Umbraco
        • Git Markdown
        • Command Prompt / Terminal
      • 👨‍💻Coding
        • Compile FrontEnd via NVM and Gulp
        • Node-Sass
        • SQL Statements
        • Build up SQL statements
        • Get current logged in user from Controller
        • Dynamic BlockList Label
        • Alt Tag
    • Umbraco
      • Luke - Version helper
      • Umbraco 10+
        • Find out Model of Current page
        • error NU1301: Unable to load the service index for source
      • Umbraco 9
        • Get the current page content type alias
      • Fluent SQL
      • Unable to open ConfigSource file
      • Delete items from backoffice by DocType ID
      • [WIP] Setting up Examine
      • Umbraco.ModelsBuilder assembly error
      • Working with IPublishedContent
      • How to Strongly Type to Models
      • Setting up a custom form
      • Getting bounced away from /umbraco
      • Examine
        • Explaining GroupedOr / GroupedAnd methods
      • Rosyln error
      • Models Builder Settings
      • Adding content to the backoffice
      • Pagination
      • Creating YYYY/MM folders
      • Configuration Error - CodeDom
    • C Sharp
      • Useful Links
      • Regex
      • Null check .any()
      • internal static and internal const
      • Dependency Injection
      • Dictionary<Tkey, TValue>
      • Linq / Lambda
  • Git
    • Git Actions
      • Build your own Git Action
      • Create a readme file automatically
    • Command line
      • Unstage a file
      • Ignore web.config changes on commit
      • Allow for case-insensitivity in Windows
      • Making VSCode your Git editor and diff tool
      • Add, Push, Pull, Clone
      • Make Terminal look nice
    • Conventional Commit
Powered by GitBook
On this page

Was this helpful?

  1. Coding
  2. C Sharp

Linq / Lambda

PreviousDictionary<Tkey, TValue>NextGit Actions

Last updated 1 year ago

Was this helpful?

The ToModel method returns a IEnumberble of SitemapNodeModel, ToModel accepted a T of IEnumberable IPublishedcontent.

I need to remove ToModel so I wrote the following :



List<SitemapNodeModel> list = new List<SitemapNodeModel>();

foreach (var child in children)
{
				SitemapNodeModel sitemap = new SitemapNodeModel
				{
					Id = child.Id,
					Name = child.Name,
					DocumentTypeAlias = child.DocumentTypeAlias,
					Url = child.Url,
					UrlName = child.UrlName,
					UpdateDate = child.UpdateDate
				};
				
				list.Add(sitemap);
}

This works but it can also be done with Linq and Lambda

var children = _umbracoWrapper.Descendants(root)
				.Where(x => _umbracoWrapper.GetPropertyValue<bool>(x, "metaSitemap"))
				.Select(child => new SitemapNodeModel
				{
					Id = child.Id,
					Name = child.Name,
					DocumentTypeAlias = child.DocumentTypeAlias,
					Url = child.Url,
					UrlName = child.UrlName,
					UpdateDate = child.UpdateDate
				}).ToList();



var umbracoNodes = new List<SitemapNodeModel>(children);