2016-03-23 10 views
0

で複数の記事を実行し、私の構文ですが、私は私のライン上のコンパイルエラーがParallel.ForEach()はここParallel.ForEach

System.Data.DataRowがタイプであるが、可変

のように使用されてい保ちます

私は確かに私はただ見落としている単純なものです。以下は私の完全な構文です、誰かが私が逃しているものを私に助けることができれば、私はそれを高く評価します!未割り当ての

使用: -

private void TryParallel() 
{ 
    Dictionary<string, string> dic = new Dictionary<string, string>(); 
    string strEndpointURL = string.Format("http://sitetosenddatato.com/post"); 
    SqlDataReader reader; 
    string strPostData = ""; 
    string strMessage = ""; 
    DataSet grds = new DataSet(); 
    grds = GetSQLResults(); 
    if (grds.Tables[0].Rows.Count >= 1) 
    { 
    Parallel.ForEach(DataRow, grds.Tables[0].Rows => 
    { 
     dic.Add("userID", reader.GetValue(0).ToString()); 
     dic.Add("name", reader.GetValue(1).ToString()); 
     dic.Add("address", reader.GetValue(2).ToString()); 
     dic.Add("city", reader.GetValue(3).ToString()); 
     dic.Add("state", reader.GetValue(4).ToString()); 
     dic.Add("zip", reader.GetValue(5).ToString()); 
     dic.Add("Phone", reader.GetValue(6).ToString()); 
    }); 
    } 
    System.Web.Script.Serialization.JavaScriptSerializer json = new System.Web.Script.Serialization.JavaScriptSerializer(); 
    foreach (var d in dic) { strPostData += d.Key + "=" + Server.UrlEncode(d.Value) + "&"; } 
    strPostData += "hs_context="; 
S ystem.Net.HttpWebRequest r = (System.Net.HttpWebRequest)System.Net.WebRequest.Create(strEndpointURL); 
    r.Method = "POST"; 
    r.Accept = "application/json"; 
    r.ContentType = "application/x-www-form-urlencoded"; 
    r.ContentLength = strPostData.Length; 
    r.KeepAlive = false; 
    using (System.IO.StreamWriter sw = new System.IO.StreamWriter(r.GetRequestStream())) 
    { 
    try { sw.Write(strPostData); } 
    catch (Exception ex) { strMessage = ex.Message; } 
    } 
    var response = r.GetResponse(); 
    Stream receiveStream = response.GetResponseStream(); 
    StreamReader readStream = new StreamReader(receiveStream, Encoding.UTF8); 
    var result = readStream.ReadToEnd(); 
    var xml = System.Xml.Linq.XElement.Parse(result); 
    if (xml.Elements("success").FirstOrDefault().Value == "1") { strMessage = "Success"; } 
    else 
    { 
    var errors = xml.Elements("errors"); 
    foreach (var error in errors.Elements("error")) { strMessage = error.Value; } 
    } 
} 

EDIT
は@Glenトーマスで下記に概説の例に続いて、私はのコンパイルエラーを提示し

if (grds.Tables[0].Rows.Count == 1) 
{ 
    Parallel.ForEach(rows, row => 
    { 
    dic.Add("userID", reader.GetValue(0).ToString()); 
    //More Here 
    } 
} 

に私のコードを変更しましたローカル変数 'リーダー'

しかし、私はreaderを私の方法の先頭に宣言しましたか?

+0

@コーディングゴリラParallel.ForEachの下にあるコードの大部分は、彼が実際に何を達成するために必要なのでしょうか? –

+0

RE:未定義のリーダー - 宣言しましたが、インスタンス化されていません。コンパイラは何も割り当てないと判断し、コンパイルを拒否します。これは実行時に 'NullReferenceException'を受け取る安全機構です。 – CodingGorilla

+0

もう一度、答えは例外です - あなたが何かを割り当てない限り、コンパイラは読者の使用を受け入れません。 –

答えて

5

最初のパラメータとして型名を指定しています。これは、反復処理を行うコレクションである必要があります。 2番目のパラメータは、コレクション内の各要素のパラメータを使用して実行する関数です。

Parallel.ForEachの正しい使用方法は、このようなものです:あなたのコードの場合

var rows = new DataRow[0] 

Parallel.ForEach(rows, row => 
{ 
    // Do something with row here 
}); 

Parallel.ForEach(grds.Tables[0].Rows.OfType<DataRow>(), row => 
{ 
    dic.Add("userID", reader.GetValue(0).ToString()); 
    dic.Add("name", reader.GetValue(1).ToString()); 
    dic.Add("address", reader.GetValue(2).ToString()); 
    dic.Add("city", reader.GetValue(3).ToString()); 
    dic.Add("state", reader.GetValue(4).ToString()); 
    dic.Add("zip", reader.GetValue(5).ToString()); 
    dic.Add("Phone", reader.GetValue(6).ToString()); 
}); 
+0

OPのオリジナルコードを含むように回答を更新することを検討することがあります。 – CodingGorilla

+0

@Glen Thomas - 私の編集を参照してください。最初のエラーを取り除いた構文を使用しましたが、新しいエラーが表示されます... –

+2

あなたのコードに無関係なエラーがたくさんあります。私はあなたに尋ねる必要がある多くの質問があると思います... –

1

私はあなたの代わりにこれをしたいと考えている:

Parallel.ForEach(grds.Tables[0].Rows.OfType<DataRow>(), (row) => 
    { 
     dic.Add("userID", reader.GetValue(0).ToString()); 
     dic.Add("name", reader.GetValue(1).ToString()); 
     dic.Add("address", reader.GetValue(2).ToString()); 
     dic.Add("city", reader.GetValue(3).ToString()); 
     dic.Add("state", reader.GetValue(4).ToString()); 
     dic.Add("zip", reader.GetValue(5).ToString()); 
     dic.Add("Phone", reader.GetValue(6).ToString()); 

     //though realistically you should be doing something with your specific row 
    }); 

あなたが受け取ったエラーメッセージに答えがあります - DataRowがオブジェクトとして定義されていませんコードではを提供しました。

しかし、これも実際に私は、並列に複数のHTTPポストを実行していると信じて、あなたの実際の問題を解決していない - ので、あなたのParallel.ForEachの匿名関数(内あなたのポストのロジックを配置する必要があるだろう)

+3

私はこれがコンパイルするとは思わない。 ForEachの2番目のパラメータは、パラメータを持つデリゲートをとる必要があります。 –

+0

@GlenThomasいいキャッチ。初心者の方は、簡単なParallel.ForEach() - > https://msdn.microsoft.com/en-gb/library/dd460720(v=vs.110).aspxの記事をご覧ください。公平になるには、 OPのコードにも問題があります。 –

関連する問題