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

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

  • 相关阅读:
    返回数组指针的函数形式
    zoj 2676 网络流+01分数规划
    2013 南京理工大学邀请赛B题
    poj 2553 强连通分支与缩点
    poj 2186 强连通分支 和 spfa
    poj 3352 边连通分量
    poj 3177 边连通分量
    poj 2942 点的双连通分量
    poj 2492 并查集
    poj 1523 求割点
  • 原文地址:https://www.cnblogs.com/0banana0/p/2484695.html
Copyright © 2011-2022 走看看