zoukankan      html  css  js  c++  java
  • linq返回dynamic类型的匿名实体

    这样会报错:

    void Main()
    {
        var  x=GetSpareInfoByCode();
        Console.Write(x.Key);//报错:“object”未包含“Key”的定义
    }
        public dynamic  GetSpareInfoByCode(){
        var words =
            from word in "The quick brown fox jumps over the lazy dog".Split()
            orderby word.ToUpper()
            select word;
            
        var duplicates =
            from word in words
            group word.ToUpper() by word.ToUpper() into g
            where g.Count() > 1
            select new { g.Key, Count = g.Count() };
                return duplicates;
            }

    使用tolist

    void Main()
    {
        //var  x=GetSpareInfoByCode();
        IEnumerable<object> y=GetSpareInfoByCode();
        //foreach (var item in (IEnumerable<object>)x)
        foreach (var item in y)
        {
            Console.Write(item.GetType().GetProperty("Key").GetValue(item, null));
            Console.Write(item.GetType().GetProperty("Count").GetValue(item, null));
        }
    }
        public dynamic  GetSpareInfoByCode(){
        var words =
            from word in "The quick brown fox jumps over the lazy dog".Split()
            orderby word.ToUpper()
            select word;
            
        var duplicates =
            from word in words
            group word.ToUpper() by word.ToUpper() into g
            where g.Count() > 1
            select new { g.Key, Count = g.Count() };
                return duplicates.ToList();
            }

    有木有更简单高效的方式呢

  • 相关阅读:
    后期生成事件命令copy /y
    SevenZipShaper压缩类
    vs2017
    WCF路由服务
    微服务--
    各种流程图的绘画网路工具 processon
    ROC 准确率,召回率 F-measure理解(转载)
    Unix OpenCV安装
    转载:tar 解压缩命令~
    cppreference经验总结
  • 原文地址:https://www.cnblogs.com/0banana0/p/2484695.html
Copyright © 2011-2022 走看看