I have this piece of code that I’m trying to convert to lambda –
foreach (var facet in response.Result.Facets)
{
var newFacet = new Facet {Parent = facet.Title};
foreach (var element in facet.Subelements)
{
newFacet.Items.Add(new Facet
{
Title = element.Title,
TotalResults = element.TotalResults
});
}
searchModel.Facets.Add(newFacet);
}
Here’s what I have so far –
response.Result.Facets.ForEach(x => searchModel.Facets.Add(new Facet
{
Parent = x.Title,
Items = ???//x.Subelements.ForEach(y=>)
}));
And the classes –
public class Facet
{
public Facet()
{
Items = new List<Facet>();
}
public string Parent { get; set; }
public List<Facet> Items { get; set; }
public int TotalResults { get; set; }
public string Title { get; set; }
}
public class SearchElement
{
public string Parent { get; set; }
public string Title { get; set; }
public int TotalResults { get; set; }
public IList<ESearchElement> Subelements { get; set; }
}
How do I bind List<SearchElement> to List<Items> by mapping each element (title = y.Title..) all in one line within a lambda expression? Is it possible?
Try something like this:
EDIT: Lambda version per request in comment.
Much the same as with LINQ. Is this what you were after?