1
votes

New to C# and appreciate any help. The issue is that I need to filter the results of my api call against an array (using an "allowedA" and "allowedB" array.) I don't know how to edit the lambda expression to check against the loop.

 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();

I have been reading about lambda expressions and foreach loops so please don't just post a random link.

Thanks in advance.

2
Did you try activities.Where(a => a.ParentId == type.Id && allowedB.Contains(a.Id))? You may need to change .Contains() with IndexOf(a.id) != -1, just try. - Adriano Repetti

2 Answers

1
votes

Filter the values before Selecting.

            activityTypes.Where(x=>allowedA.Contains(x.Id)).Select(type => new CategoryViewModel
            {
                Id = type.Id,
                Text = type.Name,
                Types = activities.Where(a => a.ParentId == type.Id &&  allowedB.Contains(a.Id)).Select(t => new TaxonomyMemberTextItem
                {
                    Id = t.Id, 
                    Text = t.Name
                }).ToList()
            })
0
votes

To filter you use .Where. You .Select to create a list of new types. So in order to filter, then create the lists of objects you want:

var result = activityTypes.Where(type=>isAllowed(type.Id)).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&&isAllowed(a.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();