The following error is reported:
Void main () {var x = getspareinfobycode (); console. write (X. key); // error: "object" does not contain the definition of "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 ;}
Use 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(); }
Is there a simpler and more efficient way?