Neu in C# und jede Hilfe zu schätzen wissen. Das Problem ist, dass ich die Ergebnisse meines API-Aufrufs gegen ein Array filtern muss (mit einem Array "allowedA" und "allowedB"). Ich weiß nicht, wie ich den Lambda-Ausdruck bearbeiten soll, um ihn gegen die Schleife zu prüfen.Filtern und Hinzufügen von Werten mit C# unter Verwendung des Lambda-Ausdrucks
var activities = await _restClientTaxonomy.GetTaxonomyFullAsync(TAXONOMY_CLASSIFICATIONID_FOR_ACTIVITY);
var activityTypes = await _restClientTaxonomy.GetTaxonomyFullAsync(TAXONOMY_CLASSIFICATIONID_FOR_ACTIVITY_TYPES);
var documentEventxx = activities.Select(type => type.Id);
long [] allowedA = new long []{ 7137, 40385637};
long [] allowedB = new long []{ 7137, 40385637};
foreach (long value in documentEventxx)
{
foreach (var item in allowed)
{
if (item == value) {
//These are the values I am looking for -> values that are part of the documentEventxx and allowedB.
}
}
}
var result = activityTypes.Select(type => new CategoryViewModel
{
Id = type.Id,//This is where I want to add only items that are in the allowedA array
Text = type.Name,
Types = activities.Where(a => a.ParentId == type.Id).Select(t => new TaxonomyMemberTextItem
{
Id = t.Id, //This is where I want to add only items that are in the allowedB array
Text = t.Name
}).ToList()
}).ToArray();
Ich habe über Lambda Ausdrücke und foreach Schleifen gelesen, also bitte nicht nur eine zufällige Verbindung.
Vielen Dank im Voraus.
Versuchen Sie activities.Where (a => a.ParentId == type.Id && allowedB.Contains (a.Id))? Möglicherweise müssen Sie .Contains() mit IndexOf (a.id)! = -1 ändern, versuchen Sie es einfach. –