简体   繁体   English

从 Enumerable.WhereSelectEnumerableIterator 到 List<string>

[英]From Enumerable.WhereSelectEnumerableIterator to List<string>

I need to feed a method with a collection of type List<string> coming from ApiDescription objects:我需要使用来自ApiDescription对象的List<string>类型集合提供一个方法:

public class AddTags : IOperationFilter
{
   public void Apply(Operation operation, OperationFilterContext context)
   {
      if (context.ApiDescription.TryGetMethodInfo(out MethodInfo methods) == true)
      {
         var test = methods.CustomAttributes
            .Where(x => x.AttributeType == typeof(CustomTag))
            .Select(x => x.NamedArguments.Where(n => n.MemberName.Equals("Tag"))
                          .Select(t => (string)t.TypedValue.Value))
            .ToList();
      }
   }
}

The test variable returns a collection of type System.Linq.Enumerable.WhereSelectEnumerableIterator<System.Reflection.CustomAttributeNamedArgument, string> but I need a result of type List<string> . test变量返回System.Linq.Enumerable.WhereSelectEnumerableIterator<System.Reflection.CustomAttributeNamedArgument, string>类型的集合,但我需要List<string>类型的结果。

The custom tag is applied like this in controllers:自定义标签在控制器中是这样应用的:

[CustomTag(Tag = "TEST")]
public ActionResult MyAction()

Try this:尝试这个:

var tags = methods.GetCustomAttributes(typeof(CustomTag), true)
                  .OfType<CustomTag>()
                  .Select(attribute => attribute.Tag)
                  .ToList();

By the way, CustomTag should really be named CustomTagAttribute .顺便说一下, CustomTag应该真正命名为CustomTagAttribute It will still work as [CustomTag] .它仍然可以用作[CustomTag]

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 使用 LINQ 从列表中删除重复项,得到 System.Linq.Enumerable+WhereSelectEnumerableIterator`2 - Removeing duplicates from a list with LINQ, getting System.Linq.Enumerable+WhereSelectEnumerableIterator`2 对Xdocument的Linq查询返回“System.linq.Enumerable + WhereSelectEnumerableIterator&#39;2 [system.XML.Linq.Xelement,System.String] - Linq query on Xdocument returns "System.linq.Enumerable+WhereSelectEnumerableIterator'2[system.XML.Linq.Xelement,System.String] Enumerable + WhereSelectEnumerableIterator`2错误并使用LINQ语句获取JObject的值 - Enumerable+WhereSelectEnumerableIterator`2 Error And Use LINQ statement to get values of JObject 为什么Lambda表达式会返回System.Linq.Enumerable + WhereSelectEnumerableIterator`2 - Why Lambda expression returns System.Linq.Enumerable+WhereSelectEnumerableIterator`2 as a result 使用Linq过滤通用列表的WhereSelectEnumerableIterator - Filter WhereSelectEnumerableIterator of Generic List using Linq 如何将记录存储到 Enumerable model 列表中的字符串数组中 - How can I store the record into string array from Enumerable model list 如何从Enumerable.Any和List.Contains中获取字符串值包含比较? - How to get the string value from a Enumerable.Any and List.Contains comparison? 来自Enumerable.Range的A到Z的char列表 - A to Z list of char from Enumerable.Range 如何从可枚举列表中以列表的形式获取分组结果? - How to get grouping result as a list from the enumerable list? 填充列表<dynamic>来自 Enumerable.Range 或列表<int> - populating List<dynamic> from an Enumerable.Range or a List<int>
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM