2017-03-20 16 views
0

3つのforeachループを使用してpowershellから単一の値を取得して行に追加しようとしています。私のコードは次の通りです:複数のforeachループを一緒に繰り返してデータグリッドビューに追加するより良い方法C#

string listDomains = "Get-MsolDomain"; 
string getLicenseInfo = "Get-MsolAccountSku"; 
string domainsInfo = executeCommand.runAzurePowerShellModule(listDomains, "Name"); 
string availableLicensesTypes = executeCommand.runAzurePowerShellModule(getLicenseInfo, "AccountSkuId"); 
string totalLicenses = executeCommand.runAzurePowerShellModule(getLicenseInfo, "ActiveUnits"); 
string consumedLicenses = executeCommand.runAzurePowerShellModule(getLicenseInfo, "ConsumedUnits");  
List<string> licenseTypeArray = new List<string>(); 
List<string> totalLicenseArray = new List<string>(); 
List<string> consumedLicenseArray = new List<string>(); 

foreach (string s in Regex.Split(availableLicensesTypes, "\n")) 
{ 
    licenseTypeArray.Add(s); 
} 
foreach (string v in Regex.Split(totalLicenses, "\n")) 
{ 
    totalLicenseArray.Add(v); 
} 
foreach (string t in Regex.Split(consumedLicenses, "\n")) 
{ 
    consumedLicenseArray.Add(t); 
} 

私はこれもできますか?私は3つのリストを連結してから分割してみましたが、連結した後は何も考えられませんでした。どんな助けでも本当に感謝しています。

+0

その後、これらの値を3列のデータグリッドビューに追加したいとします。 – Nilay

+0

データはどのように整理されていますか? 3つの配列すべてが同じ数の行を持つことが保証されていますか?最初のライセンスタイプのエントリが最初の合計に対応して消費されるように注文されているので、 "type | total | consumed"のような列がありますか? – dlatikay

+0

私は(1)あなたがここで求めているもの、または(2)単にToListを呼んでいない理由を理解していません。なぜあなたのプログラムは 'var licenseTypeArray = Regex.Split(availableLicensesTypes、" \ n ")。ToList();というようになりますか?質問を明確にすることはできますか? –

答えて

0

これを処理する完全なコードは次のとおりです。コードをテストしたところ、正常に動作しています。任意のaspxページでこのコードをコピーしてテストしてください。探しているデータ変換を実行した後、サンプルデータをグリッドビューに表示します。

 using System; 
     using System.Collections.Generic; 
     using System.Linq; 
     using System.Web; 
     using System.Web.UI; 
     using System.Web.UI.WebControls; 

     namespace SampleTest1 
     { 
      public partial class _Default : Page 
      { 
       protected void Page_Load(object sender, EventArgs e) 
       { 
        List<string> licenseTypeArray = new List<string>() { "Type 1", "Type 2", "Type 3" }; 
        List<string> totalLicenseArray = new List<string>() { "100", "200", "300" }; 
        List<string> consumedLicenseArray = new List<string>() { "50", "100", "150" }; 

        //A generic list of the new entity class that wraps the three properties (columns) 
        List<LicenseInfo> licensesList = new List<LicenseInfo>(); 

       //concat zip the three lists with a comma-separated for each entry in the new list with this pattern ("License Type, Total Count, Consumed Count"). 
       //Example("Entrprise License,200,50") 
       List<string> licensesConcatenatedList = licenseTypeArray.Zip(totalLicenseArray.Zip(consumedLicenseArray, 
      (x, y) => x + "," + y), 
      (x1, y1) => x1 + "," + y1).ToList(); 


       licensesConcatenatedList.ForEach(t => licensesList.Add(new LicenseInfo 
       { 
        LicenseType = t.Split(new char[] { ',' })[0], 
        TotalLicenesesCount = int.Parse(t.Split(new char[] { ',' })[1]), 
        ConsumedLicensesCount = int.Parse(t.Split(new char[] { ',' })[2]) 
       })); 

       GridView1.DataSource = licensesList; 
       GridView1.DataBind(); 
     } 
    } 

     class LicenseInfo 
     { 
      public string LicenseType { get; set; } 
      public int TotalLicenesesCount { get; set; } 
      public int ConsumedLicensesCount { get; set; } 
     } 
    } 

私はこれがあなたに役立つことを望みます。

+0

こんにちは、私はついにそれを働かせました。 PowerShellの出力から空白のエントリがあり、これがエラーを出していました。このコードをありがとうございます。それは完璧に働いています。 – Nilay

+0

それは助けて、それが今働いていることを知って非常にうれしいです。これが役に立つと分かった場合は、回答としてマークしてください。 –

1

代わりにアイテムを反復処理のAddRangeを使用することができます。

licenseTypeArray.AddRange(Regex.Split(availableLicensesTypes, "\n")); 
totalLicenseArray.AddRange(Regex.Split(totalLicenses, "\n")); 
consumedLicenseArray.AddRange(Regex.Split(consumedLicenses, "\n")); 

私は、これはあなたが簡単な方法でどのような意味であると思います。もしあなたの質問を詳しく述べてください。

4

あなたがここで求めていることを理解するのは難しいです。

var s1 = "a b c".Split(' '); 
    var s2 = "d e f".Split(' '); 
    var s3 = "g h i".Split(' '); 

あなたは3つのシーケンスがあると言っていると思います。そしてそれらを「垂直に」連結したいと思います。

public static IEnumerable<string> ZipConcat(IEnumerable<string> xs, IEnumerable<string> ys) 
{ 
    return xs.Zip(ys, (x, y) => x + y); 
} 

そして今、あなたの問題は簡単です:

var s4 = ZipConcat(ZipConcat(s1, s2), s3); 
    foreach(var s in s4) 
     Console.WriteLine(s); 

が生成されます

adg 
beh 
cfi 
1

あなたが使用しないようにこのコードを使用することができます

あなたはジップ化連結操作を必要としますループ用に3つ、コード行を短くする

 licenseTypeArray = Regex.Split(availableLicensesTypes, "\n").ToList(); 
     totalLicenseArray = Regex.Split(totalLicenses, "\n").ToList(); 
     consumedLicenseArray = Regex.Split(consumedLicenses, "\n").ToList(); 

このデータをGridViewなどで表示するには、すべてのライセンス名をリストする最初の列と、使用可能であるか消費しているかを示す2番目の列(ブール値[trueまたは偽)])。

OK、次のコードを試してください。まず、次のような新しいエンティティクラスを定義します。次のコードを使用

 public class LicenseInfo 
     { 
      public string LicenseType { get; set; } 
      public int TotalLicenesesCount { get; set; } 
      public int ConsumedLicensesCount { get; set; } 
     } 

その後:今、あなたはあなたがGridViewの中に表示したいすべての情報をラップするエンティティの一つのリストにあなたのデータを持っている

 List<string>licenseTypeArray = Regex.Split(availableLicensesTypes, "\n").ToList(); 
     List<string> totalLicenseArray = Regex.Split(totalLicenses, "\n").ToList(); 
     List<string> consumedLicenseArray = Regex.Split(consumedLicenses, "\n").ToList(); 

     //A generic list of the new entity class that wraps the three properties (columns) 
     List<LicenseInfo> licensesList = new List<LicenseInfo>(); 

     //concat zip the three lists with a comma-separated for each entry in the new list with this pattern ("License Type, Total Count, Consumed Count"). 
     //Example("Entrprise License,200,50") 
     List<string> licensesConcatenatedList = licenseTypeArray.Zip(totalLicenseArray.Zip(consumedLicenseArray, 
      (x, y) => x +","+ y), 
      (x1,y1) => x1 + "," + y1).ToList(); 


     licensesConcatenatedList.ForEach(t => licensesList.Add(new LicenseInfo 
     { 
      LicenseType = t.Split(new char[] { ',' })[0], 
      TotalLicenesesCount = int.Parse(t.Split(new char[] { ',' })[1]), 
      ConsumedLicensesCount = int.Parse(t.Split(new char[] { ',' })[2]) 
     })); 

を、そしてちょうどこの新しいリストをバインドいつものようにデータソースとしてgridview。 GridViewのフィールド名としてプロパティの名前を使用します。

アイデアがはっきりしていることを願っています。これがあなたを助けたらそれを回答としてマークしてください。

+0

3つの列を表示する必要があります。ライセンスの種類、ライセンスの総数、消費されたライセンスの合計数。どのようにこれらの行を3つの配列のdatagridview foreach値に追加するのですか? – Nilay

+0

ありがとうございます。私はしかし、エラーが発生しています。それは正しい形式ではない入力文字列です。 – Nilay

+0

申し訳ありませんが私はここで間違っている場合、私はまだC#を学んでいます。私はgridviewのために以下のコードを使用しています:var source = new BindingSource(); source.DataSource = licensesConcatenatedList; licenseDataGridView.DataSource = source; – Nilay

関連する問題