2010年1月31日日曜日

[C#] List.ConvertAll

C# 3.0以上で出来ます。(VBでも出来るはず)

Listの値をCSV文字列に変換出来ます。

//Listをカンマ区切りの文字列に変換する
List lst = new List { 1, 3, 5, 7 };

string csv =
string.Join(",", lst.ConvertAll(
delegate(int value) { return value.ToString(); }).ToArray());


//カンマ区切りの文字列をListに変換する
lst = csv.Split(',').ToList().ConvertAll(
delegate(string value) { return int.Parse(value); });



1行で書くことが良いかは別として出来るんですね。
# 勝手にHTMLタグが書き込まれて消せない。。。

2009年11月15日日曜日

[IIS6.0] Webガーデン

IIS6.0導入されたWebガーデンについてのメモです。

Webガーデンで最大ワーカープロセス数を2以上にした場合に注意が必要。
Webガーデンとは、1つのアプリケーションを複数のワーカープロセスで動かす仕組み。
アプリケーション変数や、セッションは共有されない。
つまり、SessionIdは別物になる。
Session領域も別物になる。
システム構築時にクラスタ構成と同じように考慮する必要がある。

あんな簡単に設定できるものが、こんなに大きく影響があってよいのですかねぇ。

2009年11月11日水曜日

[ASP.NET MVC] リストのモデルバインド

ASP.NET MVCでモデルバインドが便利っぽいような不便っぽいような。
新規にリストの値をバインドするなら問題ないのですよ。

Model

Public Class PrivateInfo
Public Name As String
Public History As List(Of History)
End Class

Public Class History
Public eventDate As DateTime
Public Score As Int32
Public Cost As Int32
End Class


View

<%
For each item In Model.History
Dim index As Integer = Model.History.IndexOf(item)
%>
<%=Html.TextBox(String.Format("History[{0}].Score", index), item.Score)%>
<%=Html.TextBox(String.Format("History[{0}].Cost", index), item.Cost)%>

<% Next%>


ところがですね、すでにある程度の値が入っているリストに画面でeventDateを設定させようとしてUpdateModelすると、ScoreとCostが消えちゃうのです。
View

<%
For each item In Model.History
Dim index As Integer = Model.History.IndexOf(item)
%>
<%=Html.TextBox(String.Format("History[{0}].eventDate", index), item.eventDate)%>

<% Next%>


Controller

<AcceptVerbs(HttpVerbs.Post)> _
Public Function Input2(ByVal collection As FormCollection) As ActionResult
Dim model As Models.PrivateInfo = Session("PrivateInfo")

UpdateModel(model)

Return View(model)
End Function



そこまでやってくれないんですね。
対策としては、その他の項目もPostしちゃうと。

View

<%
For each item In Model.History
Dim index As Integer = Model.History.IndexOf(item)
%>
<%=Html.TextBox(String.Format("History[{0}].eventDate", index), item.eventDate)%>
<%=Html.Hidden(String.Format("History[{0}].Score", index), item.Score)%>
<%=Html.Hidden(String.Format("History[{0}].Cost", index), item.Cost)%>

<% Next%>



イケテナイ。
自分でマージするか