使用C#使用lambda表达式过滤并添加值

使用C#使用lambda表达式过滤并添加值,c#,arrays,foreach,lambda,C#,Arrays,Foreach,Lambda,C#新手,非常感谢您的帮助。问题是我需要根据数组(使用“allowedA”和“allowedB”数组)过滤api调用的结果。我不知道如何编辑lambda表达式来检查循环 var activities = await _restClientTaxonomy.GetTaxonomyFullAsync(TAXONOMY_CLASSIFICATIONID_FOR_ACTIVITY); var activityTypes = await _restClientTaxonomy.GetTaxonomyF

C#新手,非常感谢您的帮助。问题是我需要根据数组(使用“allowedA”和“allowedB”数组)过滤api调用的结果。我不知道如何编辑lambda表达式来检查循环

 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();
我一直在阅读有关lambda表达式和foreach循环的文章,所以请不要只是发布一个随机链接


提前感谢。

要过滤,请使用
。Where
。您可以
。选择
创建新类型的列表。因此,为了进行筛选,然后创建所需对象的列表:

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

在选择之前过滤这些值

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

您是否尝试了活动。其中(a=>a.ParentId==type.Id&&allowedB.Contains(a.Id))?您可能需要使用IndexOf(a.id)更改.Contains()-1,只需try.BTW,在选择TaxonomyMemberTextItem的地方,使用一个名为
t
的lambda变量,但它应该表示源类型,就像在它前面的where中一样,以便更具可读性,所以请尝试使用
a