'How to use lambda expression for below code block?
I am writing a piece of code in order to get values from the server. There are multiple code lines and need to do the same thing using lambda expressions. This is the code I have tried and please help to rewrite this code using lambda expressions.
var newDocuments = result?.DocumentQueryResults.OrderBy(d => context.GetParams().GetAllDocumentIds().ToList().IndexOf(d.Document.Id)).ToList();
var test = new List<HealthRecord>();
foreach (DocumentQueryResult item in newDocuments)
{
test.Add(item.Document);
}
Solution 1:[1]
I believe you're looking for something like this:
// Cache the sort parameters, as suggested by Caius Jard:
var sortParams = context.GetParams()
.GetAllDocumentIds()
.AsEnumerable()
.Select((id, index) => new { id, index })
.ToDictionary(x => x.id, x => x.index);
var test = result?.DocumentQueryResults
.OrderBy(d => sortParams[d.Document.Id])
.Select(d => d.Document)
.ToList();
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | Richard Deeming |
