2012-04-16 1 views
1

私はグループのリストを持つリストサービスを持っています。各グループにはGroupNameがあり、クライアント側には、それらのGroupNameを変数groupboxのリストに追加しようとしていますGroupNames誰もがコードを支援することができます)私の残りのグループのサービスであり?:可変内容がwebserviceによって設定されています

 string uriGroups = "http://localhost:8000/Service/Group"; 
     XDocument xDoc = XDocument.Load(uriGroups); 
     var groups = xDoc.Descendants("Group") 
     .Select(n => new 
     { 
      GroupBox groupbox = new GroupBox(); 
      groupbox.Header = String.Format("Group #{0}", n.Element("GroupName"); 
      groupbox.Width = 100; 
      groupbox.Height = 100; 
      groupbox.Margin = new Thickness(2); 

      StackPanel stackPanel = new StackPanel(); 
      stackPanel.Children.Add(groupbox); 
      stackPanel.Margin = new Thickness(10); 

      MainArea.Children.Add(stackPanel); 
     } 

このありえない、私はちょうどそれを行う方法で立ち往生しています正しいです。

EDIT:

public Reports() 
    { 
     InitializeComponent(); 

     string uriGroups = "http://localhost:8000/Service/Group"; 
     XDocument xDoc = XDocument.Load(uriGroups); 
     foreach(var node in xDoc.Descendants("Group")) 
     { 

      GroupBox groupbox = new GroupBox(); 
      groupbox.Header = String.Format("Group #{0}", node.Element("Name")); 
      groupbox.Width = 100; 
      groupbox.Height = 100; 
      groupbox.Margin = new Thickness(2); 

      StackPanel stackPanel = new StackPanel(); 
      stackPanel.Children.Add(groupbox); 
      stackPanel.Margin = new Thickness(10); 

      MainArea.Children.Add(stackPanel); 
     } 

    } 
    public static IEnumerable<T> ForEach<T>(this IEnumerable<T> enumerable, Action<T> action) 
    { 
     foreach (var item in enumerable) 
      action(item); 

     return enumerable; 
    } 

答えて

2

1)あなたは、コレクションを反復処理すると何かをするLINQ Select拡張子を使用するべきではありません。要素を新しい形式に変換するためにのみ使用する必要があります。あなたはこのような何かをしたい場合は、どちらかだけforeach文を使用し、またはそうのように列挙上で動作するように新しいLINQの拡張を行います。それは文法的に壊れているため、上記のコードはコンパイルされません)

public static IEnumerable<T> ForEach<T>(this IEnumerable<T> enumerable, Action<T> action) 
    { 
     foreach(var item in enumerable) 
      action(item); 

     return enumerable; 
    } 

2 。あなたがしようとしているのは新しい匿名型(new { })です。このオブジェクトに対してプロパティを作成せず、(許可されていない)ランダムなコード行を実行しようとしているので、これは無効です。匿名型を作成するときは、このようなものだろう:

Enumerable.Range(0, 10).Select(x => new { Number = x }); 
// Creates a series of 10 objects with a Number property 

3)あなたはこのを成し遂げるために適切なコードにコードをリファクタリングする必要があるようですね。私はあなたが持っている特定の問題を見ていない、非コンパイル以外の部分。

+0

私はちょうどあなたが言ったことを実装する方法はわからない、コードに固執しています:S –

+0

たとえば、 '選択'を使う代わりに 'foreach(xDoc.Descendents(" Group "))'を呼び出し、コード内で 'n'の代わりに' item'を使います。 – Tejs

+0

しかし、system.LinqにはDescendentsの定義が含まれていませんか? –

関連する問題