tag:blogger.com,1999:blog-62125155595412237862024-03-19T22:06:44.072-07:00C# and ASP.NET CodeC# and .NET tips and code snippetsErik K.http://www.blogger.com/profile/04738667434492418561noreply@blogger.comBlogger53125tag:blogger.com,1999:blog-6212515559541223786.post-55053859909235645992014-12-15T08:48:00.000-08:002014-12-15T08:50:10.787-08:00Editor Template for Boolean Property in RazorIf you want to display 2 radio buttons for a boolean property in Razor with Yes and No labels, this is how you could do it.<br />
<br />
Create your own editor template and put it in the Shared/EditorTemplate folder.<br />
<br />
Template code:<br />
<br />
@model bool<br />
@using System.Web.Mvc<br />
@Html.RadioButtonFor(model=>model, false) No<br />
@Html.RadioButtonFor(model => model, true) Yes<br />
<br />Erik K.http://www.blogger.com/profile/04738667434492418561noreply@blogger.com16tag:blogger.com,1999:blog-6212515559541223786.post-31177679440051950852014-10-23T07:20:00.001-07:002014-10-23T07:21:52.649-07:00Create Separate Dropdowns for Day, Month, Year in ASP.NET MVCCreate a structure that will hold the Month, Day and Year fields:<br />
<br />
<pre style="background: #f6f8ff; color: #000020;"><html><body style="background: #f6f8ff; color: #000020;"><pre><span style="color: #200080; font-weight: bold;">public</span> <span style="color: #200080; font-weight: bold;">struct</span> DatePart
<span style="color: #406080;">{</span>
<span style="color: #200080; font-weight: bold;">public</span> <span style="color: #200080; font-weight: bold;">static</span> IEnumerable<span style="color: #308080;"><</span><span style="color: #200080; font-weight: bold;">int</span><span style="color: #308080;">></span> DayItems
<span style="color: #406080;">{</span>
get
<span style="color: #406080;">{</span>
<span style="color: #200080; font-weight: bold;">return</span> Enumerable<span style="color: #308080;">.</span>Range<span style="color: #308080;">(</span><span style="color: #008c00;">1</span><span style="color: #308080;">,</span> <span style="color: #008c00;">31</span><span style="color: #308080;">)</span><span style="color: #406080;">;</span>
<span style="color: #406080;">}</span>
<span style="color: #406080;">}</span>
<span style="color: #200080; font-weight: bold;">public</span> <span style="color: #200080; font-weight: bold;">static</span> IEnumerable<span style="color: #308080;"><</span>String<span style="color: #308080;">></span> MonthItems
<span style="color: #406080;">{</span>
get
<span style="color: #406080;">{</span>
<span style="color: #200080; font-weight: bold;">return</span> <span style="color: #200080; font-weight: bold;">new</span> System<span style="color: #308080;">.</span>Globalization<span style="color: #308080;">.</span>DateTimeFormatInfo<span style="color: #308080;">(</span><span style="color: #308080;">)</span><span style="color: #308080;">.</span>MonthNames<span style="color: #406080;">;</span>
<span style="color: #406080;">}</span>
<span style="color: #406080;">}</span>
<span style="color: #200080; font-weight: bold;">public</span> <span style="color: #200080; font-weight: bold;">static</span> IEnumerable<span style="color: #308080;"><</span><span style="color: #200080; font-weight: bold;">int</span><span style="color: #308080;">></span> YearItems
<span style="color: #406080;">{</span>
get
<span style="color: #406080;">{</span>
<span style="color: #200080; font-weight: bold;">return</span> Enumerable<span style="color: #308080;">.</span>Range<span style="color: #308080;">(</span>DateTime<span style="color: #308080;">.</span>Now<span style="color: #308080;">.</span>Year <span style="color: #308080;">-</span> <span style="color: #008c00;">30</span><span style="color: #308080;">,</span> <span style="color: #008c00;">30</span> <span style="color: #308080;">+</span> <span style="color: #008c00;">1</span><span style="color: #308080;">)</span><span style="color: #406080;">;</span>
<span style="color: #406080;">}</span>
<span style="color: #406080;">}</span>
<span style="color: #406080;">}</span>
</pre></body></html></pre>Your model class should contain the following properties;<br />
<br />
<pre style="background: #f6f8ff; color: #000020;"><html><body style="background: #f6f8ff; color: #000020;"><pre><span style="color: #200080; font-weight: bold;">public</span> <span style="color: #200080; font-weight: bold;">int</span> Day <span style="color: #406080;">{</span> get<span style="color: #406080;">;</span> set<span style="color: #406080;">;</span> <span style="color: #406080;">}</span>
<span style="color: #200080; font-weight: bold;">public</span> <span style="color: #200080; font-weight: bold;">int</span> Month <span style="color: #406080;">{</span> get<span style="color: #406080;">;</span> set<span style="color: #406080;">;</span> <span style="color: #406080;">}</span>
<span style="color: #200080; font-weight: bold;">public</span> <span style="color: #200080; font-weight: bold;">int</span> Year <span style="color: #406080;">{</span> get<span style="color: #406080;">;</span> set<span style="color: #406080;">;</span> <span style="color: #406080;">}</span>
</pre></body></html></pre><br />
Create <b>SelectListItem</b> Date, Month and Year collections and add them to the <b>ViewBag</b>.<br />
<br />
<pre style="background: #f6f8ff; color: #000020;"><html><body style="background: #f6f8ff; color: #000020;"><pre>ViewBag<span style="color: #308080;">.</span>Days <span style="color: #308080;">=</span> DatePart<span style="color: #308080;">.</span>DayItems<span style="color: #308080;">.</span>Select<span style="color: #308080;">(</span>d <span style="color: #308080;">=</span><span style="color: #308080;">></span> <span style="color: #200080; font-weight: bold;">new</span> SelectListItem <span style="color: #406080;">{</span> Text <span style="color: #308080;">=</span> d<span style="color: #308080;">.</span>ToString<span style="color: #308080;">(</span><span style="color: #308080;">)</span> <span style="color: #406080;">}</span><span style="color: #308080;">)</span><span style="color: #406080;">;</span>
ViewBag<span style="color: #308080;">.</span>Months <span style="color: #308080;">=</span> DatePart<span style="color: #308080;">.</span>MonthItems<span style="color: #308080;">.</span>Select<span style="color: #308080;">(</span><span style="color: #308080;">(</span>m<span style="color: #308080;">,</span> i<span style="color: #308080;">)</span> <span style="color: #308080;">=</span><span style="color: #308080;">></span> <span style="color: #200080; font-weight: bold;">new</span> SelectListItem
<span style="color: #406080;">{</span>
Value <span style="color: #308080;">=</span> <span style="color: #308080;">(</span>i <span style="color: #308080;">+</span> <span style="color: #008c00;">1</span><span style="color: #308080;">)</span><span style="color: #308080;">.</span>ToString<span style="color: #308080;">(</span><span style="color: #308080;">)</span><span style="color: #308080;">,</span>
Text <span style="color: #308080;">=</span> m
<span style="color: #406080;">}</span><span style="color: #308080;">)</span><span style="color: #406080;">;</span>
ViewBag<span style="color: #308080;">.</span>Years <span style="color: #308080;">=</span> DatePart<span style="color: #308080;">.</span>YearItems<span style="color: #308080;">.</span>Select<span style="color: #308080;">(</span>y <span style="color: #308080;">=</span><span style="color: #308080;">></span> <span style="color: #200080; font-weight: bold;">new</span> SelectListItem <span style="color: #406080;">{</span> Text <span style="color: #308080;">=</span> y<span style="color: #308080;">.</span>ToString<span style="color: #308080;">(</span><span style="color: #308080;">)</span> <span style="color: #406080;">}</span><span style="color: #308080;">)</span><span style="color: #406080;">;</span></pre></body></html></pre>In the view, create dropdowns and populate them with collections in the ViewBag.<br />
<pre style="background: #f6f8ff; color: #000020;"><html><body style="background: #f6f8ff; color: #000020;"><pre>@Html<span style="color: #308080;">.</span>DropDownList<span style="color: #308080;">(</span><span style="color: maroon;">"</span><span style="color: #1060b6;">Day</span><span style="color: maroon;">"</span><span style="color: #308080;">,</span> <span style="color: #308080;">(</span>IEnumerable<span style="color: #308080;"><</span>SelectListItem<span style="color: #308080;">></span><span style="color: #308080;">)</span> ViewBag<span style="color: #308080;">.</span>Days<span style="color: #308080;">)</span>
@Html<span style="color: #308080;">.</span>DropDownList<span style="color: #308080;">(</span><span style="color: maroon;">"</span><span style="color: #1060b6;">Month</span><span style="color: maroon;">"</span><span style="color: #308080;">,</span> <span style="color: #308080;">(</span>IEnumerable<span style="color: #308080;"><</span>SelectListItem<span style="color: #308080;">></span><span style="color: #308080;">)</span> ViewBag<span style="color: #308080;">.</span>Months<span style="color: #308080;">)</span>
@Html<span style="color: #308080;">.</span>DropDownList<span style="color: #308080;">(</span><span style="color: maroon;">"</span><span style="color: #1060b6;">Year</span><span style="color: maroon;">"</span><span style="color: #308080;">,</span> <span style="color: #308080;">(</span>IEnumerable<span style="color: #308080;"><</span>SelectListItem<span style="color: #308080;">></span><span style="color: #308080;">)</span> ViewBag<span style="color: #308080;">.</span>Years<span style="color: #308080;">)</span>
</pre></body></html></pre>It is important that the select element names match the property names in the model. </br><br />
Now, in your controller method you can access the selected values as follows:<br />
<pre style="background: #f6f8ff; color: #000020;"><html><body style="background: #f6f8ff; color: #000020;"><pre>var selectedTime <span style="color: #308080;">=</span> <span style="color: #200080; font-weight: bold;">new</span> DateTime<span style="color: #308080;">(</span>model<span style="color: #308080;">.</span>Year<span style="color: #308080;">,</span> model<span style="color: #308080;">.</span>Month<span style="color: #308080;">,</span> model<span style="color: #308080;">.</span>Day<span style="color: #308080;">)</span><span style="color: #406080;">;</span>
</pre></body></html></pre>Erik K.http://www.blogger.com/profile/04738667434492418561noreply@blogger.com15tag:blogger.com,1999:blog-6212515559541223786.post-3464935265844321822014-02-27T08:25:00.000-08:002014-12-30T08:39:03.757-08:00Replace foreach with Extension Methods and Lambda ExpressionsI suppose many of us use the <b style="color: blue;">foreach </b>statement very often in our code. I'll show you how to replace simple <b><span style="color: blue;">foreach </span></b>statements with new <a href="http://msdn.microsoft.com/en-us/library/bb397687.aspx">lambda expressions</a>.<br />
<br />
Suppose you want to create a List < string> of names of files residing in a directory. This is how you could do it.<br />
<br />
Set up an array of FileInfo objects:<br />
<br />
<div style="background: white; color: black; font-family: Consolas; font-size: 11pt;">
<div style="margin: 0px;">
<span style="color: blue;">string</span> filePath = <span style="color: #a31515;">@"C:\Temp"</span>;</div>
<div style="margin: 0px;">
<span style="color: #2b91af;">DirectoryInfo</span> dInfo = <span style="color: blue;">new</span> <span style="color: #2b91af;">DirectoryInfo</span>(filePath); </div>
<div style="margin: 0px;">
<span style="color: #2b91af;">FileInfo</span>[] fileInfoArray = dInfo.GetFiles();</div>
</div>
<br />
Create a string type List:<br />
<br />
<div style="background: white; color: black; font-family: Consolas; font-size: 11pt;">
<div style="margin: 0px;">
<span style="color: #2b91af;">List</span><<span style="color: blue;">string</span>> fileList = <span style="color: blue;">new</span> <span style="color: #2b91af;">List</span><<span style="color: blue;">string</span>>();</div>
<div style="margin: 0px;">
<span style="color: blue;">foreach</span> (<span style="color: #2b91af;">FileInfo</span> fi <span style="color: blue;">in</span> fileInfoArray)</div>
<div style="margin: 0px;">
{</div>
<div style="margin: 0px;">
fileList.Add(fi.Name);</div>
<div style="margin: 0px;">
}<br />
<br /></div>
</div>
<h3>
Select</h3>
Using lambda expressions and the <a href="http://msdn.microsoft.com/en-us/library/bb548891%28v=VS.100%29.aspx">Select </a>extension method it is possible to re-write the last 3 lines as follows:<br />
<br />
<div style="background: white; color: black; font-family: Consolas; font-size: 11pt;">
<div style="margin: 0px;">
<span style="color: blue;">var</span> files = fileInfoArray.<b>Select</b>(fi => fi.Name);</div>
</div>
<br />
That's one line instead of 3. But we still want a List <string> object to be used later in the code. We need to use the <a href="http://msdn.microsoft.com/en-us/library/bb342261%28v=VS.100%29.aspx">Enumerable.ToList <TSource> method </a>to cast to a List. This is how we do it:<br />
<br />
<div style="background: white; color: black; font-family: Consolas; font-size: 11pt;">
<div style="margin: 0px;">
<span style="color: #2b91af;">List</span><<span style="color: blue;">string</span>> fileList2 = fileInfoArray.Select(fi => fi.Name).ToList (); </div>
</div>
<br />
This way you use one line of code instead of 4.<br />
<br />
<b>Modify Elements in a Collection</b><br />
<br />
Use the <b>Select</b> method to modify elements in a collection. The important part is you need to <b>return </b>each element;<br />
<br />
An example follows:<br />
<br />
var plans = query.<b>AsEnumerable</b>().Select(it =><br />
{<br />
it.PlanYears = Enumerable.Range(it.Plan.StartDate.Year, (DateTime.Now.Year - it.Plan.StartDate.Year) + 1); <br />
<b>return </b>it;<br />
});<br />
<br />
<b>How to get an ordinal position of an item in the list.</b><br />
Example:<br />
<br />
<pre class="csharp" style="font-family: monospace;"><span style="color: #0600ff; font-weight: bold;">var</span> mm <span style="color: green;">=</span> <span style="color: green;">new</span> <span style="color: black;">System.<span style="color: blue;">Globalization</span></span><span style="color: green;">.</span><span style="color: blue;">DateTimeFormatInfo</span><span style="color: green;">(</span><span style="color: green;">)</span><span style="color: green;">.</span><span style="color: blue;">MonthNames</span><span style="color: green;">.</span><span style="color: #0600ff; font-weight: bold;">Select</span><span style="color: green;">(</span> <span style="color: green;">(</span>a, i<span style="color: green;">)</span> <span style="color: green;">=></span> <span style="color: green;">new</span>
<span style="color: green;">{</span>
Item <span style="color: green;">=</span> a,
Position <span style="color: green;">=</span> i
<span style="color: green;">}</span><span style="color: green;">)</span><span style="color: green;">;</span></pre>
<h3>
</h3>
<h3>
Where</h3>
Now, what if we add only those file names to the List that meet certain criteria, e.g:<br />
<br />
<div style="background: white; color: black; font-family: Consolas; font-size: 11pt;">
<div style="margin: 0px;">
<span style="color: blue;">foreach</span> (<span style="color: #2b91af;">FileInfo</span> fi <span style="color: blue;">in</span> fileInfoArray)</div>
<div style="margin: 0px;">
{</div>
<div style="margin: 0px;">
<span style="color: blue;">if</span> (fi.Length > 10000000)</div>
<div style="margin: 0px;">
{</div>
<div style="margin: 0px;">
largeFiles.Add(fi.Name);</div>
<div style="margin: 0px;">
}</div>
<div style="margin: 0px;">
}</div>
</div>
<br />
Which equivalent extension method can we use to achieve the same result? We need to use the <a href="http://msdn.microsoft.com/en-us/library/bb534803%28v=VS.100%29.aspx">Where</a> extension method of the IEnumerable <T> interface first, and then append the earlier Select method to the statement:<br />
<br />
<div style="background: white; color: black; font-family: Consolas; font-size: 11pt;">
<div style="margin: 0px;">
<span style="color: blue;">var</span> varLargeFiles = fileInfoArray.<b>Where </b>(fi => fi.Length > 1000000).Select(fi=>fi.Name);</div>
</div>
<br />
<br />
<div style="margin: 2px;">
<script type="text/javascript">
<!--
google_ad_client = "ca-pub-8504512626105280";
/* 300x250, created 12/9/10 */
google_ad_slot = "7115539031";
google_ad_width = 300;
google_ad_height = 250;
//</script>
</div>
-->
<script src="http://pagead2.googlesyndication.com/pagead/show_ads.js" type="text/javascript">
</script><br />
<br />
<h3>
FindAll</h3>
If you need to filter your collection and iterate through the result, use the <b>FindAll </b>extension:<br />
<br />
IncomeSourceList.FindAll(item => item.ItemSourceType == SourceType.PERM).ForEach(<br />
<br />
item => Debug.WriteLine(item.ItemSourceType)<br />
);<br />
<br />
This code selects only those collection items whose ItemSourceType equals to PERM type, and then loops through the result.<br />
<h3>
LINQ</h3>
Now, to complete the example, I'll show the way to obtain a similar list using the <a href="http://msdn.microsoft.com/library/bb308959.aspx"><b>LINQ</b></a>:<br />
<br />
<div style="background: white; color: black; font-family: Consolas; font-size: 11pt;">
<div style="margin: 0px;">
<span style="color: blue;">var</span> q = <span style="color: blue;">from</span> fi <span style="color: blue;">in</span> fileInfoArray</div>
<div style="margin: 0px;">
<span style="color: blue;">where</span> fi.Length > 1000000</div>
<div style="margin: 0px;">
<span style="color: blue;">select</span> fi.Name;</div>
<div style="margin: 0px;">
<br /></div>
<div style="margin: 0px;">
<span style="color: #2b91af;">List</span><<span style="color: blue;">string</span>> largeFiles2 = q.ToList();</div>
</div>
<br />
Query operator <b>ToList() </b>forces immediate query evaluation.<br />
<br />
<h4>
<span style="font-size: large;"><b>Boolean Methods </b></span></h4>
<br />
What if you have a method that loops through an array and returns a <b>bool </b>type depending on some condition inside the loop. Can we use query operators instead? It is slightly more difficult but still possible.<br />
<br />
Suppose you have the following methods that compares each character in a string array to each character in another string array and returns false when a character in the second array is not found in the first array:<br />
<br />
<br />
<div style="background: white; color: black; font-family: Consolas; font-size: 11pt; padding-bottom: 8px; padding-left: 8px; padding-right: 8px; padding-top: 8px;">
<div style="margin: 0px;">
<span style="color: blue;">public</span><span style="font-weight: bold;"> </span><span style="color: blue;">static</span><span style="font-weight: bold;"> </span><span style="color: blue;">bool</span><span style="font-weight: bold;"> </span>IsAnagram<span style="font-weight: bold;">(</span><span style="color: blue;"></span><span style="font-weight: bold;"></span><span style="color: blue;">string</span><span style="font-weight: bold;"> </span>word<span style="font-weight: bold;">, </span><span style="color: blue;">string</span><span style="font-weight: bold;"> </span>input<span style="font-weight: bold;">)</span></div>
<div style="margin: 0px;">
<span style="font-weight: bold;">{</span></div>
<div style="margin: 0px;">
<span style="font-weight: bold;"> </span><span style="color: blue;">char</span><span style="font-weight: bold;">[] </span>inputArray<span style="font-weight: bold;"> </span>=<span style="font-weight: bold;"> </span>input.ToCharArray<span style="font-weight: bold;">();</span></div>
<div style="margin: 0px;">
<span style="font-weight: bold;"> </span><span style="color: blue;">foreach</span><span style="font-weight: bold;"> (</span><span style="color: blue;">char</span><span style="font-weight: bold;"> </span>ch<span style="font-weight: bold;"> </span><span style="color: blue;">in</span><span style="font-weight: bold;"> </span>word.ToCharArray<span style="font-weight: bold;">())</span></div>
<div style="margin: 0px;">
<span style="font-weight: bold;"> {</span></div>
<div style="margin: 0px;">
<span style="font-weight: bold;"> </span><span style="color: blue;">if</span><span style="font-weight: bold;"> (</span>!inputArray.Contains<span style="font-weight: bold;">(</span>ch<span style="font-weight: bold;">))</span></div>
<div style="margin: 0px;">
<span style="font-weight: bold;"> </span><span style="color: blue;">return</span><span style="font-weight: bold;"> </span><span style="color: blue;">false</span><span style="font-weight: bold;">;</span></div>
<div style="margin: 0px;">
<span style="font-weight: bold;"> }</span></div>
<div style="margin: 0px;">
<span style="font-weight: bold;"> </span><span style="color: blue;">return</span><span style="font-weight: bold;"> </span><span style="color: blue;">true</span><span style="font-weight: bold;">;</span></div>
<div style="margin: 0px;">
<span style="font-weight: bold;">}</span></div>
<div style="margin: 0px;">
</div>
</div>
<br />
To emulate similar statements, first we need to find the first non-matching character:<br />
<br />
<div style="background: white; color: black; font-family: Consolas; font-size: 11pt; padding-bottom: 8px; padding-left: 8px; padding-right: 8px; padding-top: 8px;">
<div style="margin: 0px;">
<span style="color: blue;">char</span><span style="font-weight: bold;"> </span>c<span style="font-weight: bold;"> </span>=<span style="font-weight: bold;"> </span>word.ToCharArray<span style="font-weight: bold;">()</span>.FirstOrDefault<span style="font-weight: bold;"> (</span>x<span style="font-weight: bold;"> </span>=><span style="font-weight: bold;"> </span>!inputArray.Contains<span style="font-weight: bold;">(</span>x<span style="font-weight: bold;">)); </span></div>
<div style="margin: 0px;">
<span style="font-weight: bold;"> </span></div>
</div>
We use the <i><b>FirstOrDefault </b></i>operator instead of <b><i>First </i></b>because otherwise we may have "Sequence contains no matching element" error if the expression does not returns any characters. <br />
<br />
Now we can return a <b>bool </b>value depending on the result of the previous statement:<br />
<div style="background: white; color: black; font-family: Consolas; font-size: 11pt; padding-bottom: 8px; padding-left: 8px; padding-right: 8px; padding-top: 8px;">
<div style="margin: 0px;">
<span style="color: blue;">return</span><span style="font-weight: bold;"> (</span>c<span style="font-weight: bold;"> </span>!=<span style="font-weight: bold;"> </span>0<span style="font-weight: bold;">) </span>?<span style="font-weight: bold;"> </span><span style="color: blue;">false</span><span style="font-weight: bold;">: </span><span style="color: blue;">true</span><span style="font-weight: bold;">;</span></div>
</div>
<br />
For sheer fun you can rewrite those two statements as follows: <br />
<div style="background-color: white; background-position: initial initial; background-repeat: initial initial; padding: 8px;">
<div style="margin: 0px;">
<div style="color: black; font-family: Consolas; font-size: 11pt;">
<span style="color: blue;">return</span><span style="font-weight: bold;"> (</span>word.ToCharArray<span style="font-weight: bold;">()</span>.FirstOrDefault<span style="font-weight: bold;">(</span>x<span style="font-weight: bold;"> </span>=><span style="font-weight: bold;"> </span>!inputArray.Contains<span style="font-weight: bold;">(</span>x<span style="font-weight: bold;">))) </span>==<span style="font-weight: bold;"> </span>0<span style="font-weight: bold;">;</span></div>
<div style="color: black; font-family: Consolas; font-size: 11pt;">
<br /></div>
<h4 style="color: black; font-family: 'Times New Roman'; font-size: medium;">
<strong style="font-family: Consolas; font-size: 14.44444465637207px;"><span style="font-size: large;">Grouping</span></strong></h4>
<span style="font-family: Consolas;"><span style="font-size: 14.44444465637207px;">Grouping allows us to group the result into distinct groups using a field or several fields as group criteria. Each group will have a key property that you can use to refer to the field(s) grouped on.</span></span><br />
<span style="font-family: Consolas;"><span style="font-size: 14.44444465637207px;"><br />
</span></span> <span style="font-family: Consolas;"><span style="font-size: 14.44444465637207px;">Example:</span></span><br />
<span style="font-family: Consolas;"><span style="font-size: 14.44444465637207px;"><br />
</span></span> <span style="font-family: Consolas;"><span style="font-size: 14.44444465637207px;"> Let's say we have a Sales table with the following fields: Rep, SalesDate, RepEmail, etc. We</span></span><span style="font-family: Consolas; font-size: 14.44444465637207px;"> want to know how many sales each Rep made each month.</span><br />
<span style="font-family: Consolas;"><span style="font-size: 14.44444465637207px;"><br />
</span></span> <span style="font-family: Consolas;"><span style="font-size: 14.44444465637207px;">First, let's build a query that counts the total number of sales per Rep:</span></span><br />
<br />
<pre style="background: #002240; color: white;"><span style="color: #ff9d00;">from</span> s <span style="color: #ff9d00;">in</span> Sales
group s by <span style="color: #ff628c;">s</span>.<span style="color: #ff628c;">Rep</span> into gr
<span style="color: #ff9d00;">select</span> new {
Name <span style="color: #ff9d00;">=</span> <span style="color: #ff628c;">gr</span>.<span style="color: #ff628c;">Key</span>,
Count <span style="color: #ff9d00;">=</span> <span style="color: #ff628c;">gr</span>.<span style="color: #ff628c;">Count</span>()
}
</pre>
<br />
<span style="font-family: Consolas;"><span style="font-size: 14.44444465637207px;"><br />
</span></span> <span style="font-family: Consolas;"><span style="font-size: 14.44444465637207px;">This query would translate into the following sql statement:</span></span><br />
<br />
<pre style="background: #002240; color: white;"><span style="color: #ff9d00;">select</span> Rep <span style="color: #ff9d00;">as</span> Name, <span style="color: #ffb054;">Count</span>(<span style="color: #ff9d00;">*</span>)
<span style="color: #ff9d00;">from</span> Sales
<span style="color: #ff9d00;">Group By</span> Rep
</pre>
</div>
</div>
<br />
<span style="font-family: Consolas;"><span style="font-size: 14.44444465637207px;">To group the result further by month, we would use the following query:</span></span><br />
<br />
<pre style="background: #002240; color: white;"><span style="color: #ff9d00;">from</span> s <span style="color: #ff9d00;">in</span> Sales
group s by new {<span style="color: #ff628c;">s</span>.<span style="color: #ff628c;">Rep</span>, <span style="color: #ff628c;">s</span>.<span style="color: #ff628c;">SalesDate</span>.Month}
into gr
<span style="color: #ff9d00;">select</span> new {
Name <span style="color: #ff9d00;">=</span> <span style="color: #ff628c;">gr</span>.<span style="color: #ff628c;">Key</span>.Rep,
SalesMonth <span style="color: #ff9d00;">=</span> <span style="color: #ff628c;">gr</span>.<span style="color: #ff628c;">Key</span>.Month,
Count <span style="color: #ff9d00;">=</span> <span style="color: #ff628c;">gr</span>.<span style="color: #ff628c;">Count</span>()
}
</pre>
<br />
<br />
<div style="color: black; font-family: Consolas; font-size: 11pt;">
<strong><span style="font-size: large;">Sorting</span></strong></div>
<div style="color: black; font-family: Consolas; font-size: 11pt;">
<br /></div>
<div style="color: black; font-family: Consolas; font-size: 11pt;">
This does not really belong in this post, however, here is a quick way to sort a list of objects. Your object could be similar to this one:</div>
<div style="color: black; font-family: Consolas; font-size: 11pt;">
<br /></div>
<br />
<pre style="background: #002240; color: white;"><span style="color: #ffee80;">class</span> <span style="color: #ffdd00;">MyObject</span>{
<span style="color: #ff9d00;">public</span> string Symbol {<span style="color: #ffb054;">get</span>;<span style="color: #ffb054;">set</span>;}
<span style="color: #ff9d00;">public</span> double Value {<span style="color: #ffb054;">get</span>;<span style="color: #ffb054;">set</span>;}
}
myList.Sort((a, b) <span style="color: #ff9d00;">=</span>> a.Symbol.CompareTo(b.Symbol));
</pre>
<br />
<br />
<strong style="font-family: Consolas; font-size: 14.5454540252686px;"><span style="font-size: large;">Any</span></strong><br />
<br />
To find out if an element exists in a collection, use the extension method <b>Any</b>:<br />
<pre style="background-color: white; margin: 0em; overflow: auto;"><code style="color: black; font-family: Consolas,"Courier New",Courier,Monospace; font-size: 10pt;"> if(userRoles.<b>Any</b>(role=> role.Equals(<span style="color: #a31515;">"Admin"</span>)))</code></pre>
<pre style="background-color: white; margin: 0em; overflow: auto;"><code style="color: black; font-family: Consolas,"Courier New",Courier,Monospace; font-size: 10pt;"> ...</code></pre>
<br />
<br />
<span style="color: purple;">Some other posts you might find interesting:</span><br />
<pre style="background-color: white; background-position: initial initial; background-repeat: initial initial; color: black; font-family: Consolas; font-size: 11pt;"><span style="color: purple;"><a href="http://c-sharpe.blogspot.ca/2010/06/extension-method-first.html" target="_blank">Query Operator First</a></span></pre>
<pre style="background-color: white; background-position: initial initial; background-repeat: initial initial; color: black; font-family: Consolas; font-size: 11pt;"><span style="color: purple;"><a href="http://c-sharpe.blogspot.ca/2009/06/easy-syntax-to-print-list-elements.html" target="_blank">Easy Syntax to Print List Elements</a></span></pre>
<pre style="background-color: white;"></pre>
<pre style="background-color: white;"></pre>
<pre style="background-color: white; background-position: initial initial; background-repeat: initial initial; color: black; font-family: Consolas; font-size: 11pt;"><span style="color: purple;">
</span></pre>
Erik K.http://www.blogger.com/profile/04738667434492418561noreply@blogger.com6tag:blogger.com,1999:blog-6212515559541223786.post-57139047560816096512013-09-04T06:22:00.003-07:002013-09-04T06:23:10.805-07:00Deploy ASP MVC on IIS 6This post just complements information already available online.<br />
There are certain steps you need to do first before your ASP MVC application can run in IIS 6.<br />
<br />
In the <b>Properties </b>screen, on the <b>Directory </b>tab click <b>Configuration</b>.<br />
<br />
<div class="separator" style="clear: both; text-align: center;">
<a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEg9l9padVv6-7l2z8KkQbGQT7lPqaFsjcncAx9ev17x6vrTrAh5No3vY4bWzBvf0T0JeVQIBZCKxAXz2QPUS8svmacHJuG18caMKJpEFJeCipQmErpFStvdwO5wvp1iOpxlmX3-m0w1oOk/s1600/config1.png" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"><img border="0" height="320" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEg9l9padVv6-7l2z8KkQbGQT7lPqaFsjcncAx9ev17x6vrTrAh5No3vY4bWzBvf0T0JeVQIBZCKxAXz2QPUS8svmacHJuG18caMKJpEFJeCipQmErpFStvdwO5wvp1iOpxlmX3-m0w1oOk/s320/config1.png" width="290" /></a></div>
<br />
On the <b>Mappings </b>tab, click <b>Insert</b> to insert a wildcard application map:<br />
<br />
<div class="separator" style="clear: both; text-align: center;">
<a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEg3nn_e85ir6XfUYHQMXwANUmN3ymjq1xhmJLlTGxRAyA4V5Rf7qVjhHdvQkyxPp56hm8wz9340Ov1pZSqdFflpxjLm6_59DH703bSUzDdEIA6NIG0mjVtI0P2AfAM5WnmHxDUBJhNCFNI/s1600/ExtensionMapping.png" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"><img border="0" height="97" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEg3nn_e85ir6XfUYHQMXwANUmN3ymjq1xhmJLlTGxRAyA4V5Rf7qVjhHdvQkyxPp56hm8wz9340Ov1pZSqdFflpxjLm6_59DH703bSUzDdEIA6NIG0mjVtI0P2AfAM5WnmHxDUBJhNCFNI/s320/ExtensionMapping.png" width="320" /></a></div>
<br />
Browse to the location of the aspnet_isapi.dll file. Make sure that <b>Verify that file exists</b> check-box is clear.<br />
<br />
Click OK to save your changes. That should be enough provided you have all the required dll files in the bin folder.<br />
<br />Erik K.http://www.blogger.com/profile/04738667434492418561noreply@blogger.com0tag:blogger.com,1999:blog-6212515559541223786.post-75793782667662229622013-07-18T11:17:00.002-07:002013-07-18T11:17:38.422-07:00Copy Property ValuesIf you have 2 objects that do not inherit from each other but have similar properties, here is a function you can use to copy property values from one object to the other:<br />
<code><br />
public static void CopyIdenticalProperties (this object source, object dest)<br />
{<br />
var plist = from prop in source.GetType().GetProperties()<br />
where prop.CanRead && prop.CanWrite<br />
select prop;<br />
<br />
foreach (PropertyInfo prop in plist)<br />
{<br />
var destProp = dest.GetType().GetProperty(prop.Name); <br />
if (destProp != null)<br />
{<br />
destProp.SetValue(dest, prop.GetValue(source, null), null);<br />
}<br />
}<br />
}<br />
</code>Erik K.http://www.blogger.com/profile/04738667434492418561noreply@blogger.com1tag:blogger.com,1999:blog-6212515559541223786.post-23208464911656003102013-06-12T12:00:00.000-07:002013-06-12T12:01:13.490-07:00Format Phone NumberThis is how you could format a phone number using a period as a separator:<br />
<br />
<span style="font-family: Courier New, Courier, monospace;"><span style="color: blue;">String.Format</span>(<span style="color: #666666;">"{0:###\\.###\\.####}"</span>, 1234567890);</span><br />
<br />
The resulting string will look as follows: <span style="font-family: Courier New, Courier, monospace;">123.456.7890.</span>Erik K.http://www.blogger.com/profile/04738667434492418561noreply@blogger.com1tag:blogger.com,1999:blog-6212515559541223786.post-37915454871063240862012-11-28T10:30:00.001-08:002012-11-28T10:32:59.624-08:00Update WCF Service Site ReferencesIf you have a problem updating dll files in the bin folder of your Web Site deployment project, the following method may work: delete all dll files in your bin folder, and then add a reference to you Service project. All the other required dll files will be added automatically.Erik K.http://www.blogger.com/profile/04738667434492418561noreply@blogger.com1tag:blogger.com,1999:blog-6212515559541223786.post-48402902539160599722012-05-31T13:14:00.001-07:002012-06-12T10:20:27.599-07:00Regex for Month NumberThis is the regular expressions pattern I use to test whether string characters represent a month number (from 01 to 12):<br />
<br />
<span style="color: blue; font-size: large;">(0[1-9]|1[0-2])</span><br />
<br />
The expression part inside the brackets is broken into 2 possible parts divided by a vertical bar (or pipe symbol): <span style="font-size: large;">0[1-9] <span style="font-size: small;">or</span> 1[0-2]</span>.<br />
<br />
If a string matches any of the 2 parts it is true.<br />
<br />
The first one says that the 2-character string has to start with 0 and end with any number in the range from 1 to 9, e.g. <em>01, 02, 09</em><br />
<br />
The second part matches any 2-character string that starts with 1 and ends with 0, 1 or 2, i.e. <em>10, 11, or 12</em>Erik K.http://www.blogger.com/profile/04738667434492418561noreply@blogger.com1tag:blogger.com,1999:blog-6212515559541223786.post-1437876798217332032012-03-12T08:00:00.000-07:002012-05-14T08:45:10.922-07:00Delete Contents of IsolatedStorageFileThe easiest way to delete contents of an IsolatedStorageFile is to open it in the Truncate mode:<br />
<br />
<span style="color: #2b91af;">IsolatedStorageFile</span> isoStore = <span style="color: #2b91af;">IsolatedStorageFile</span>.GetUserStoreForAssembly();<br />
<br />
<span style="color: #2b91af;">IsolatedStorageFileStream</span> isoFile = <span style="color: blue;">new </span><span style="color: #2b91af;">IsolatedStorageFileStream</span>(<em>myFileName</em>, <span style="color: #2b91af;">FileMode</span>.Truncate, isoStore);<br />
<br />
According to the <a href="http://msdn.microsoft.com/en-us/library/system.io.filemode.aspx" target="_blank">MSDN</a> , if a file is opened in the Truncate mode its size should be 0 bytes.Erik K.http://www.blogger.com/profile/04738667434492418561noreply@blogger.com1tag:blogger.com,1999:blog-6212515559541223786.post-82290421087373820792011-12-02T11:17:00.001-08:002012-06-12T09:58:24.450-07:00Delete ScriptManager HistoryWell, I have not found a way to clear <strong>ScriptManager</strong> history proper, but the workaround is to use the <strong>AddHistoryPoint</strong> method and set the value of your property to "0":<br />
<br />
ScriptManager1.AddHistoryPoint(<span style="color: #a31515;">"myKey"</span>, <span style="color: #a31515;">"0"</span>);<br />
<br />
<br />Erik K.http://www.blogger.com/profile/04738667434492418561noreply@blogger.com1tag:blogger.com,1999:blog-6212515559541223786.post-963415519343915412011-10-25T10:59:00.000-07:002014-04-07T09:59:25.035-07:00Check if GridView Column ExistsIf you have a <span style="color: #2b91af;">DataRowView </span>you can use the following extension method to check if a GridView column exists:<br />
<br />
<em>public static bool ColumnExists( <span style="color: blue;">this </span><span style="color: #2b91af;">DataRowView </span>rowData, <span style="color: blue;">string </span>fldToCheck)<br />{</em><br />
<em><span style="color: blue;">return </span>rowData.Row.Table.Columns.Contains(fldToCheck);</em><br />
<em>}</em><br />
<br />
Normally, you would use it on RowDataBound event, e.g:<br />
<br />
<em>protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)<br />{</em><br />
<em><span style="color: #2b91af;">DataRowView </span>rowData = e.Row.DataItem as DataRowView;</em><br />
<em><span style="color: blue;">string </span>fldToCheck = "MyFieldName";</em><br />
<em>if (e.Row.RowType == DataControlRowType.DataRow)</em><br />
<em>{</em><br />
<em> if (rowData.ColumnExists(fldToCheck) ...</em><br />
<br />
<br />
Related posts:<br />
<a href="http://c-sharpe.blogspot.ca/2011/10/find-gridview-column-index.html" target="_blank">Find GridView Column Index</a><br />
<a href="http://c-sharpe.blogspot.ca/2010/10/set-datarow-values.html" target="_blank">Set DataRow Values and Other ASP.NET GridView Tips</a><br />
<h3 class="post-title entry-title" itemprop="name" style="background-color: white; color: #222222; font-family: Arial, Tahoma, Helvetica, FreeSans, sans-serif; font-size: 22px; font-weight: normal; margin: 0.75em 0px 0px; position: relative;">
</h3>
<div style="float: left; margin-right: 4px;">
<script type="text/javascript">
<!--
google_ad_client = "ca-pub-8504512626105280";
/* 300x250, created 12/9/10 */
google_ad_slot = "7115539031";
google_ad_width = 300;
google_ad_height = 250;
//</script>
</div>
<br />
<script src="http://pagead2.googlesyndication.com/pagead/show_ads.js" type="text/javascript">
</script>Erik K.http://www.blogger.com/profile/04738667434492418561noreply@blogger.com1tag:blogger.com,1999:blog-6212515559541223786.post-88676910131185998292011-10-17T13:58:00.000-07:002014-04-07T11:19:52.672-07:00Modify Query String<br />
If you build a hyperlink on a web page dynamically, sometimes you may need to append or modify an existing query string.<br />
To modify a url with an existing query string, first grab the AbsoluteUrl property:<br />
<br />
<span style="color: blue;">string</span> path = Request.Url.AbsolutePath ;<br />
<br />
This will give you a url wihout the query string portion. <br />
<br />
If you need to reuse an existing portion of the query string, you have to get it yourself, e.g:<br />
<br />
<span style="color: blue;">string</span> key = Request.QueryString[<span style="color: #a31515;">"key"</span>];<br />
<span style="color: blue;">string</span> path = Request.Url.AbsolutePath + "?key="+ key;<br />
<br />
<br />
Now, you can build your url:<br />
<br />
<span style="color: blue;">string</span> url = path+ "&key2="+ myValue;<br />
<br />
Related posts:<br />
<a href="http://c-sharpe.blogspot.ca/2010/11/add-javascript-dynamically-to.html" target="_blank">Add JavaScript Dynamically to ASP.NET UpdatePanel</a><br />
<a href="http://c-sharpe.blogspot.ca/2010/07/examine-columns-in-metatable.html" target="_blank">Examine Columns in ASP.NET Dynamic Data MetaTable</a><br />
<br />
<br />
<br />
<br />Erik K.http://www.blogger.com/profile/04738667434492418561noreply@blogger.com1tag:blogger.com,1999:blog-6212515559541223786.post-32690929880254441132011-10-13T13:21:00.000-07:002012-06-12T09:59:31.559-07:00Find GridView Column IndexIf you create G<strong>ridView</strong> columns dynamically using the <strong>AutogenerateColumns</strong> = true feature, sometimes you need to find a column index from the column name.<br />
<br />
I created an extension method to find a column index:<br />
<br />
<pre style="background: #ffffff; color: black;"><span style="color: maroon; font-weight: bold;">public</span> <span style="color: maroon; font-weight: bold;">static</span> <span style="color: maroon; font-weight: bold;">int</span> GetIndex<span style="color: #808030;">(</span><span style="color: maroon; font-weight: bold;">this</span> DataRowView tableData<span style="color: #808030;">,</span> <span style="color: maroon; font-weight: bold;">string</span> fieldName<span style="color: #808030;">)</span>
<span style="color: purple;">{</span>
DataColumn dc <span style="color: #808030;">=</span> tableData<span style="color: #808030;">.</span>DataView<span style="color: #808030;">.</span>Table<span style="color: #808030;">.</span>Columns<span style="color: #808030;">[</span>fieldName<span style="color: #808030;">]</span><span style="color: purple;">;</span>
<span style="color: maroon; font-weight: bold;">if</span> <span style="color: #808030;">(</span>dc <span style="color: #808030;">!</span><span style="color: #808030;">=</span> <span style="color: maroon; font-weight: bold;">null</span><span style="color: #808030;">)</span>
<span style="color: purple;">{</span>
<span style="color: maroon; font-weight: bold;">return</span> dc<span style="color: #808030;">.</span>Ordinal<span style="color: purple;">;</span>
<span style="color: purple;">}</span>
<span style="color: maroon; font-weight: bold;">return</span> <span style="color: #808030;">-</span><span style="color: #008c00;">1</span><span style="color: purple;">;</span>
<span style="color: purple;">}</span>
</pre>
<br />
This is how you call from the <strong>RowDataBound</strong> event handler:<br />
<br />
<pre style="background: #ffffff; color: black;">DataRowView tableData <span style="color: #808030;">=</span> e<span style="color: #808030;">.</span>Row<span style="color: #808030;">.</span>DataItem <span style="color: maroon; font-weight: bold;">as</span> DataRowView<span style="color: purple;">;</span>
<span style="color: maroon; font-weight: bold;">int</span> pos <span style="color: #808030;">=</span> tableData<span style="color: #808030;">.</span>GetIndex<span style="color: #808030;">(</span><span style="color: maroon;">"</span><span style="color: #0000e6;">MyFieldName</span><span style="color: maroon;">"</span><span style="color: #808030;">)</span><span style="color: purple;">;</span>
<span style="color: maroon; font-weight: bold;">if</span> <span style="color: #808030;">(</span>pos <span style="color: #808030;">!</span><span style="color: #808030;">=</span> <span style="color: #808030;">-</span><span style="color: #008c00;">1</span><span style="color: #808030;">)</span><span style="color: purple;">{</span>
<span style="color: dimgrey;"> //do your thing</span>
<span style="color: purple;">}</span>
</pre>Erik K.http://www.blogger.com/profile/04738667434492418561noreply@blogger.com0tag:blogger.com,1999:blog-6212515559541223786.post-59542254232935340942011-07-27T13:26:00.000-07:002012-04-25T17:11:50.316-07:00WPF GridView Cell Borders<code>The post is moved to <a href="http://wpf123.blogspot.ca/2012/04/wpf-gridview-cell-borders.html">this </a>blog.</code>Erik K.http://www.blogger.com/profile/04738667434492418561noreply@blogger.com0tag:blogger.com,1999:blog-6212515559541223786.post-54909683400142924462011-03-17T07:24:00.000-07:002012-06-12T09:59:52.086-07:00Check QueryString for NullIf you need to determine whether the <strong>Request.QueryString</strong> is null and take some action depending on the result, it is not enough to check for null. QueryString is an <strong>HttpValueCollection</strong> type, so even if the Request.Url does not have a query string appended to it, the value of the collection is not going to be null. The proper way to check if the query string contains anything is to check for null <strong>and the Count</strong> property as well:<br />
<code><br /><span style="color: blue;">if<span style="color: black;">(Request.QueryString!= </span><span style="color: blue;">null</span><span style="color: black;"> && Request.QueryString.Count>0){</span><br /> //some piece of code.<br />}</span></code><br />
<br />
<div>
<script type="text/javascript">
<!--
google_ad_client = "ca-pub-8504512626105280";
/* 300x250, created 12/9/10 */
google_ad_slot = "7115539031";
google_ad_width = 300;
google_ad_height = 250;
//-->
</script><br />
<script src="http://pagead2.googlesyndication.com/pagead/show_ads.js" type="text/javascript">
</script></div>Erik K.http://www.blogger.com/profile/04738667434492418561noreply@blogger.com5tag:blogger.com,1999:blog-6212515559541223786.post-76434958465738351242010-12-03T12:35:00.000-08:002012-06-12T10:00:14.140-07:00C# Delegates as Method ParametersLet's say you have 2 methods that go through similar steps:<br />
<br />
<div style="background: white; color: black; font-family: Consolas; font-size: 11pt; padding-bottom: 8px; padding-left: 8px; padding-right: 8px; padding-top: 8px;">
<div style="margin: 0px;">
<span style="color: blue;">public</span><span style="font-weight: bold;"> </span><span style="color: blue;">string</span><span style="font-weight: bold;"> </span>GetPrevItem<span style="font-weight: bold;">()</span></div>
<div style="margin: 0px;">
<span style="font-weight: bold;"> {</span></div>
<div style="margin: 0px;">
<span style="font-weight: bold;"> </span><span style="color: blue;">if</span><span style="font-weight: bold;"> (</span><b>IsValidPrevEntry</b><span style="font-weight: bold;">())</span></div>
<div style="margin: 0px;">
<span style="font-weight: bold;"> {</span></div>
<div style="margin: 0px;">
<span style="font-weight: bold;"> </span><span style="color: blue;">string</span><span style="font-weight: bold;"> </span>item<span style="font-weight: bold;"> </span>=<span style="font-weight: bold;"> </span><b>GetPrevString</b><span style="font-weight: bold;">();</span></div>
<div style="margin: 0px;">
<span style="font-weight: bold;"> </span>SetProperties<span style="font-weight: bold;">();</span></div>
<div style="margin: 0px;">
<span style="font-weight: bold;"> </span><span style="color: blue;">return</span><span style="font-weight: bold;"> </span>item<span style="font-weight: bold;">;</span></div>
<div style="margin: 0px;">
<span style="font-weight: bold;"> }</span></div>
<div style="margin: 0px;">
<span style="font-weight: bold;"> </span><span style="color: blue;">else</span></div>
<div style="margin: 0px;">
<span style="font-weight: bold;"> </span><span style="color: blue;">return</span><span style="font-weight: bold;"> </span><span style="color: blue;">null</span><span style="font-weight: bold;">;</span></div>
<div style="margin: 0px;">
<span style="font-weight: bold;"> }</span></div>
<div style="margin: 0px;">
<br /></div>
<div style="margin: 0px;">
<span style="font-weight: bold;"></span><span style="color: blue;">public</span><span style="font-weight: bold;"> </span><span style="color: blue;">string</span><span style="font-weight: bold;"> </span>GetNextItem<span style="font-weight: bold;">()</span></div>
<div style="margin: 0px;">
<span style="font-weight: bold;">{</span></div>
<div style="margin: 0px;">
<span style="font-weight: bold;"> </span><span style="color: blue;">if</span><span style="font-weight: bold;"> (</span><b>IsValidNextEntry</b><span style="font-weight: bold;">())</span></div>
<div style="margin: 0px;">
<span style="font-weight: bold;"> {</span></div>
<div style="margin: 0px;">
<span style="font-weight: bold;"> </span><span style="color: blue;">string</span><span style="font-weight: bold;"> </span>item<span style="font-weight: bold;"> </span>=<span style="font-weight: bold;"> </span><b>GetNextString</b><span style="font-weight: bold;">();</span></div>
<div style="margin: 0px;">
<span style="font-weight: bold;"> </span>SetProperties<span style="font-weight: bold;">();</span></div>
<div style="margin: 0px;">
<span style="font-weight: bold;"> </span><span style="color: blue;">return</span><span style="font-weight: bold;"> </span>item<span style="font-weight: bold;">;</span></div>
<div style="margin: 0px;">
<span style="font-weight: bold;"> }</span></div>
<div style="margin: 0px;">
<span style="font-weight: bold;"> </span><span style="color: blue;">else</span></div>
<div style="margin: 0px;">
<span style="font-weight: bold;"> </span><span style="color: blue;">return</span><span style="font-weight: bold;"> </span><span style="color: blue;">null</span><span style="font-weight: bold;">;</span> </div>
<div style="margin: 0px;">
<span style="font-weight: bold;"> }</span></div>
<div style="margin: 0px;">
<span style="font-weight: bold;"> </span></div>
</div>
These 2 methods essentially do the same thing. First, they check for some condition, and if the condition evaluates to true, they call another method, set some properties, and return a string. We can use <b>C#</b> <b>delegates </b>to refactor these 2 methods into one. Here is how you do it:<br />
Declare 2 <b>delegates </b>whose signature matches the methods we want to replace:<br />
<div style="background: white; color: black; font-family: Consolas; font-size: 11pt; padding-bottom: 8px; padding-left: 8px; padding-right: 8px; padding-top: 8px;">
<div style="background: white; color: black; font-family: Consolas; font-size: 11pt; padding-bottom: 8px; padding-left: 8px; padding-right: 8px; padding-top: 8px;">
<div style="margin: 0px;">
<span style="font-weight: bold;"> </span><span style="color: blue;">delegate</span><span style="font-weight: bold;"> </span><span style="color: blue;">bool</span><span style="font-weight: bold;"> </span><span style="color: #2b91af;">IsValid</span><span style="font-weight: bold;">();</span></div>
<div style="margin: 0px;">
<span style="font-weight: bold;"> </span><span style="color: blue;">delegate</span><span style="font-weight: bold;"> </span><span style="color: blue;">string</span><span style="font-weight: bold;"> </span><span style="color: #2b91af;">GetItem</span><span style="font-weight: bold;">();</span></div>
</div>
</div>
Create a method that will use the delegates: <br />
<br />
<div style="background: white; color: black; font-family: Consolas; font-size: 11pt; padding-bottom: 8px; padding-left: 8px; padding-right: 8px; padding-top: 8px;">
<div style="margin: 0px;">
<span style="font-weight: bold;"> </span><span style="color: blue;">private</span><span style="font-weight: bold;"> </span><span style="color: blue;">string</span><span style="font-weight: bold;"> </span>GetMyItem<span style="font-weight: bold;">(</span><span style="color: #2b91af;">IsValid</span><span style="font-weight: bold;"> </span>isValid<span style="font-weight: bold;">, </span><span style="color: #2b91af;">GetItem</span><span style="font-weight: bold;"> </span>getItem<span style="font-weight: bold;">)</span></div>
<div style="margin: 0px;">
<span style="font-weight: bold;"> {</span></div>
<div style="margin: 0px;">
<span style="font-weight: bold;"> </span><span style="color: blue;">if</span><span style="font-weight: bold;"> (</span>isValid<span style="font-weight: bold;">())</span></div>
<div style="margin: 0px;">
<span style="font-weight: bold;"> {</span></div>
<div style="margin: 0px;">
<span style="font-weight: bold;"> </span><span style="color: blue;">string</span><span style="font-weight: bold;"> </span>item<span style="font-weight: bold;"> </span>=<span style="font-weight: bold;"> </span>getItem<span style="font-weight: bold;">();</span></div>
<div style="margin: 0px;">
<span style="font-weight: bold;"> </span>SetEventProperties<span style="font-weight: bold;">();</span></div>
<div style="margin: 0px;">
<span style="font-weight: bold;"> </span><span style="color: blue;">return</span><span style="font-weight: bold;"> </span>item<span style="font-weight: bold;">;</span></div>
<div style="margin: 0px;">
<span style="font-weight: bold;"> }</span></div>
<div style="margin: 0px;">
<span style="font-weight: bold;"> </span><span style="color: blue;">else</span></div>
<div style="margin: 0px;">
<span style="font-weight: bold;"> </span><span style="color: blue;">return</span><span style="font-weight: bold;"> </span><span style="color: blue;">null</span><span style="font-weight: bold;">;</span></div>
<div style="margin: 0px;">
<span style="font-weight: bold;"> } </span></div>
</div>
An alternative method signature is as follows:<br />
<div style="background: white; color: black; font-family: Consolas; font-size: 11pt; padding-bottom: 8px; padding-left: 8px; padding-right: 8px; padding-top: 8px;">
<div style="margin: 0px;">
<span style="font-weight: bold;"> </span><span style="color: blue;">private</span><span style="font-weight: bold;"> </span><span style="color: blue;">string</span><span style="font-weight: bold;"> </span>GetNavItem<span style="font-weight: bold;">(</span><span style="color: #2b91af;">Func</span><<span style="color: blue;">bool</span>><span style="font-weight: bold;"> </span>isValid<span style="font-weight: bold;">, </span><span style="color: #2b91af;">Func</span><<span style="color: blue;">string</span>><span style="font-weight: bold;"> </span>getItem<span style="font-weight: bold;">)</span></div>
</div>
This way we don't have to declare delegates separately.<br />
<br />
Now, we can call the new method, passing the methods that we called before refactoring, as parameters:<br />
<div style="background: white; color: black; font-family: Consolas; font-size: 11pt; padding-bottom: 8px; padding-left: 8px; padding-right: 8px; padding-top: 8px;">
<div style="margin: 0px;">
<span style="font-weight: bold;"></span><span style="color: blue;">string</span><span style="font-weight: bold;"> </span>strPrev<span style="font-weight: bold;"> </span>=<span style="font-weight: bold;"> </span>GetMyItem<span style="font-weight: bold;">(</span><b>IsValidPrevEntry<span style="font-weight: bold;">, </span>GetPrevString</b><span style="font-weight: bold;">);</span></div>
<div style="margin: 0px;">
<span style="font-weight: bold;"></span><span style="color: blue;">string</span><span style="font-weight: bold;"> </span>strNext<span style="font-weight: bold;"> </span>=<span style="font-weight: bold;"> </span>GetMyItem<span style="font-weight: bold;">(</span><b>IsValidNextEntry<span style="font-weight: bold;">, </span>GetNextString</b><span style="font-weight: bold;">);</span></div>
</div>
An interesting discussion of delegates can be found on the <a href="http://stackoverflow.com/questions/2113779/can-someone-explain-this-c-lambda-syntax">stackflow</a>.Erik K.http://www.blogger.com/profile/04738667434492418561noreply@blogger.com0tag:blogger.com,1999:blog-6212515559541223786.post-32104639255779120422010-11-16T08:45:00.000-08:002012-06-12T10:00:28.705-07:00Add JavaScript Dynamically to ASP.NET UpdatePanelUse the following example:<br />
<div style="background: white; color: black; font-family: Consolas; font-size: 11pt; padding-bottom: 8px; padding-left: 8px; padding-right: 8px; padding-top: 8px;">
<div style="margin: 0px;">
<br /></div>
<div style="margin: 0px;">
System.Text.<span style="color: #2b91af;">StringBuilder</span> sb = <span style="color: blue;">new</span> System.Text.<span style="color: #2b91af;">StringBuilder</span>(); </div>
<div style="margin: 0px;">
sb.Append(<span style="color: #a31515;">@" var inputs = document.getElementsByTagName('input'); for (var i = 0; i < inputs.length; i++) {</span></div>
<div style="margin: 0px;">
<span style="color: #a31515;"> if (inputs[i].type == 'text') { </span></div>
<div style="margin: 0px;">
<span style="color: #a31515;">inputs[i].onkeypress = function (event) {</span><span style="color: #a31515;"><br /> event = event || window.event; </span><br />
<span style="color: #a31515;"> return myJavaScriptFunction(event);}</span></div>
<div style="margin: 0px;">
<span style="color: #a31515;"> }}"</span></div>
<div style="margin: 0px;">
);</div>
<div style="margin: 0px;">
<span style="color: #2b91af;">ScriptManager</span>.<a href="http://msdn.microsoft.com/en-us/library/bb350750.aspx">RegisterClientScriptBlock </a>(<span style="color: blue;">this</span>, <span style="color: blue;">this</span>.GetType(), <span style="color: #a31515;">"ajax"</span>, sb.ToString(), <span style="color: blue;">true</span>); </div>
</div>
<br />
What it does is attaches a <b>JavaScript </b>function (defined elsewhere) to each input box on the page on the key press event, and then registers it for use with a control that is inside an <b>UpdatePanel</b>. This could be used for user input validation. The code has been verified in IE and Firefox.Erik K.http://www.blogger.com/profile/04738667434492418561noreply@blogger.com1tag:blogger.com,1999:blog-6212515559541223786.post-56563740497069037552010-11-10T13:28:00.000-08:002012-06-12T10:00:51.515-07:00ASP.NET GridView Subclass with Dynamic Footer TotalsA frequent requirement when using an <b>ASP.NET GridView</b> on a web page is to have a footer with totals for numeric columns in the GridView. Here is a <b>GridView</b> subclass that calculates footer totals dynamically. It requires 3 parameters:<br />
<br />
<b>FirstComputedColumnIndex</b> - the for index of the first column in the data set to be computed<br />
<b>FirstVisbleComputedColumnIndex</b> - for the index of the first computed column to be shown<br />
<b>NumberOfComput</b><b>edColumns</b> - for the number of computed columns.<br />
<br />
Here is the class code:<br />
<br />
<div style="background: white; color: black; font-family: Consolas; font-size: 11pt; padding-bottom: 8px; padding-left: 8px; padding-right: 8px; padding-top: 8px;">
<div style="margin: 0px;">
<span style="color: blue;">namespace</span> Eric.TotalGrid {</div>
</div>
<div style="background: white; color: black; font-family: Consolas; font-size: 11pt;">
<div style="margin: 0px;">
<span style="color: blue;">public</span> <span style="color: blue;">partial</span> <span style="color: blue;">class</span> <span style="color: #2b91af;">GridView</span> : System.Web.UI.WebControls.<span style="color: #2b91af;">GridView</span></div>
<div style="margin: 0px;">
{</div>
<div style="margin: 0px;">
<span style="color: blue;">private</span> Indexer footerTotals;</div>
<div style="margin: 0px;">
<span style="color: blue;">public</span> <span style="color: blue;">int</span> FirstComputedColumnIndex { <span style="color: blue;">get</span>; <span style="color: blue;">set</span>; }</div>
<div style="margin: 0px;">
<span style="color: blue;">public</span> <span style="color: blue;">int</span> FirstVisbleComputedColumnIndex { <span style="color: blue;">get</span>; <span style="color: blue;">set</span>; }</div>
<div style="margin: 0px;">
<span style="color: blue;">public</span> <span style="color: blue;">int</span> NumberOfComputedColumns { <span style="color: blue;">get</span>; <span style="color: blue;">set</span>; } </div>
<div style="margin: 0px;">
<br /></div>
<div style="margin: 0px;">
<span style="color: blue;">protected</span> <span style="color: blue;">override</span> <span style="color: blue;">void</span> OnLoad(<span style="color: #2b91af;">EventArgs</span> e)</div>
<div style="margin: 0px;">
{</div>
<div style="margin: 0px;">
ShowFooter = <span style="color: blue;">true</span>;</div>
<div style="margin: 0px;">
<span style="color: blue;">base</span>.OnLoad(e);</div>
<div style="margin: 0px;">
}</div>
<div style="margin: 0px;">
<span style="color: blue;">protected</span> <span style="color: blue;">override</span> <span style="color: blue;">void</span> OnInit(<span style="color: #2b91af;">EventArgs</span> e)</div>
<div style="margin: 0px;">
{</div>
<div style="margin: 0px;">
footerTotals = <span style="color: blue;">new</span> Indexer(NumberOfComputedColumns);</div>
<div style="margin: 0px;">
InitializeComponent();</div>
<div style="margin: 0px;">
<span style="color: blue;">base</span>.OnInit(e);</div>
<div style="margin: 0px;">
}</div>
<div style="margin: 0px;">
<br /></div>
<div style="margin: 0px;">
<span style="color: blue;">private</span> <span style="color: blue;">void</span> InitializeComponent()</div>
<div style="margin: 0px;">
{</div>
<div style="margin: 0px;">
<span style="color: blue;">this</span>.RowDataBound += <span style="color: blue;">new</span> <span style="color: #2b91af;">GridViewRowEventHandler</span>(<span style="color: blue;">this</span>.GridView1_RowDataBound);</div>
<div style="margin: 0px;">
}</div>
<div style="margin: 0px;">
<br /></div>
<div style="margin: 0px;">
<span style="color: blue;">protected</span> <span style="color: blue;">void</span> GridView1_RowDataBound(<span style="color: blue;">object</span> sender, <span style="color: #2b91af;">GridViewRowEventArgs</span> e)</div>
<div style="margin: 0px;">
{ </div>
<div style="margin: 0px;">
<br /></div>
<div style="margin: 0px;">
<span style="color: #2b91af;">DataRowView</span> tableData = e.Row.DataItem <span style="color: blue;">as</span> <span style="color: #2b91af;">DataRowView</span>;</div>
<div style="margin: 0px;">
<br /></div>
<div style="margin: 0px;">
<span style="color: blue;">if</span> (e.Row.RowType == <span style="color: #2b91af;">DataControlRowType</span>.DataRow)</div>
<div style="margin: 0px;">
{ </div>
<div style="margin: 0px;">
<span style="color: blue;">for</span> (<span style="color: blue;">int</span> i = FirstComputedColumnIndex; i < (FirstComputedColumnIndex + NumberOfComputedColumns); i++)</div>
<div style="margin: 0px;">
{ </div>
<div style="margin: 0px;">
<span style="color: blue;">if</span> (tableData[i] != <span style="color: #2b91af;">DBNull</span>.Value)</div>
<div style="margin: 0px;">
{</div>
<div style="margin: 0px;">
<span style="color: blue;">decimal</span> result = 0;</div>
<div style="margin: 0px;">
<span style="color: blue;">if</span> (<span style="color: #2b91af;">Decimal</span>.TryParse(tableData[i].ToString(), <span style="color: blue;">out</span> result))</div>
<div style="margin: 0px;">
{</div>
<div style="margin: 0px;">
footerTotals[i - FirstComputedColumnIndex] += result;</div>
<div style="margin: 0px;">
}</div>
<div style="margin: 0px;">
}</div>
<div style="margin: 0px;">
}</div>
<div style="margin: 0px;">
}</div>
<div style="margin: 0px;">
<span style="color: blue;">else</span> <span style="color: blue;">if</span> (e.Row.RowType == <span style="color: #2b91af;">DataControlRowType</span>.Footer)</div>
<div style="margin: 0px;">
{</div>
<div style="margin: 0px;">
<span style="color: blue;">for</span> (<span style="color: blue;">int</span> i = 0; i < NumberOfComputedColumns; i++)</div>
<div style="margin: 0px;">
{</div>
<div style="margin: 0px;">
<span style="color: blue;">if</span> (e.Row.Cells.Count > (i + FirstVisbleComputedColumnIndex))</div>
<div style="margin: 0px;">
e.Row.Cells[i + FirstVisbleComputedColumnIndex].Text = footerTotals[i].ToString();</div>
<div style="margin: 0px;">
}</div>
<div style="margin: 0px;">
}</div>
<div style="margin: 0px;">
}<br />
} <br />
<br />
<div>
<script type="text/javascript">
<!--
google_ad_client = "ca-pub-8504512626105280";
/* 300x250, created 12/9/10 */
google_ad_slot = "7115539031";
google_ad_width = 300;
google_ad_height = 250;
//-->
</script><br />
<script src="http://pagead2.googlesyndication.com/pagead/show_ads.js" type="text/javascript">
</script></div>
<br />
The key to this GridView is using an <a href="http://msdn.microsoft.com/en-us/library/2549tw02%28v=VS.100%29.aspx">Indexer </a>class that can be accessed like an array. Here is its listing:<br />
<br />
<br />
<div style="background: white; color: black; font-family: Consolas; font-size: 11pt; padding-bottom: 8px; padding-left: 8px; padding-right: 8px; padding-top: 8px;">
<div style="margin: 0px;">
<span style="color: blue;">class</span> <span style="color: #2b91af;">Indexer</span></div>
<div style="margin: 0px;">
{</div>
<div style="margin: 0px;">
<span style="color: blue;">private</span> <span style="color: blue;">int</span> _upperLimit; </div>
<div style="margin: 0px;">
<br /></div>
<div style="margin: 0px;">
<span style="color: blue;">private</span> <span style="color: blue;">decimal</span>[] myArray;</div>
<div style="margin: 0px;">
<br /></div>
<div style="margin: 0px;">
<span style="color: blue;">public</span> Indexer(<span style="color: blue;">int</span> limit)</div>
<div style="margin: 0px;">
{</div>
<div style="margin: 0px;">
_upperLimit = limit;</div>
<div style="margin: 0px;">
myArray = <span style="color: blue;">new</span> <span style="color: blue;">decimal</span>[_upperLimit];</div>
<div style="margin: 0px;">
}</div>
<div style="margin: 0px;">
<span style="color: blue;">public</span> <span style="color: blue;">decimal</span> <span style="color: blue;">this</span>[<span style="color: blue;">int</span> index] <span style="color: green;">// Indexer declaration</span></div>
<div style="margin: 0px;">
{</div>
<div style="margin: 0px;">
<span style="color: blue;">get</span></div>
<div style="margin: 0px;">
{</div>
<div style="margin: 0px;">
<span style="color: green;">// Check the index limits.</span></div>
<div style="margin: 0px;">
<span style="color: blue;">if</span> (index < 0 || index >= _upperLimit)</div>
<div style="margin: 0px;">
<span style="color: blue;">return</span> 0;</div>
<div style="margin: 0px;">
<span style="color: blue;">else</span></div>
<div style="margin: 0px;">
<span style="color: blue;">return</span> myArray[index];</div>
<div style="margin: 0px;">
}</div>
<div style="margin: 0px;">
<span style="color: blue;">set</span></div>
<div style="margin: 0px;">
{</div>
<div style="margin: 0px;">
<span style="color: blue;">if</span> (!(index < 0 || index >= _upperLimit))</div>
<div style="margin: 0px;">
myArray[index] = <span style="color: blue;">value</span>;</div>
<div style="margin: 0px;">
}</div>
<div style="margin: 0px;">
}</div>
<div style="margin: 0px;">
}</div>
</div>
</div>
</div>
The <b>Indexer </b>class can be nested within the <b>GridView </b>subclass.<br />
<br />
<br />
The last example shows how to use the custom <b>GridView </b>on a ASP.NET web page:<br />
First, use the register tag:<br />
<b><%@ Register Namespace="Eric.TotalGrid" TagPrefix="x" Assembly="TotalGrid" %></b><br />
<br />
Then, use it just like any other <b>GridView</b>:<br />
<br />
<b><x:GridView runat="server" ....</b><br />
<b>FirstComputedColumnIndex="4" FirstVisbleComputedColumnIndex="3" NumberOfComputedColumns="10" ></b>Erik K.http://www.blogger.com/profile/04738667434492418561noreply@blogger.com0tag:blogger.com,1999:blog-6212515559541223786.post-14246721056556108012010-10-28T08:12:00.000-07:002012-06-12T10:01:17.086-07:00Set DataRow Values and Other ASP.NET GridView TipsYou can set <b>DataRow </b>values in two ways:<br />
First, using a column name:<br />
<i>myDataRow["City"]= "London";</i><br />
<br />
Or, using an index:<br />
<i>myDataRow[2] = "London";</i><br />
<br />
Don't use <i>ItemArray </i>property to set values, it won't work.<br />
<br />
<h4 style="color: blue;">
Set Text Box Width in a GridView Bound Field:</h4>
Use <b>ControlStyle </b>properties:<br />
<br />
<asp:BoundField... ControlStyle-Width="60px"/><br />
<br />
<div style="color: blue;">
<b>Change GridViewRow Appearance: </b></div>
<br />
There are different ways to change <b>GridViewRow </b>appearance. The <a href="http://blog.devexperience.net/en/5/Change_background_color_of_GridView%27s_Rows.aspx">approach </a>I like involves using the GridViewRow <b>Style </b>property. On <b>RowDataBound </b>event use this:<br />
<br />
<div style="background: white; color: black; font-family: Consolas; font-size: 11pt; padding-bottom: 8px; padding-left: 8px; padding-right: 8px; padding-top: 8px;">
<div style="margin: 0px;">
<span style="color: blue;">if</span><span style="font-weight: bold;"> (e.Row.RowType == </span><span style="color: #2b91af;">DataControlRowType</span>.Footer)</div>
<div style="margin: 0px;">
<span style="font-weight: bold;"> { </span></div>
<div style="margin: 0px;">
<span style="font-weight: bold;"> e.Row.Style.Add(</span><span style="color: #a31515;">"color"</span><span style="font-weight: bold;">, </span><span style="color: #a31515;">"#999999"</span><span style="font-weight: bold;">);</span></div>
<div style="margin: 0px;">
<span style="font-weight: bold;"> e.Row.Style.Add(</span><span style="color: #a31515;">"font-weight"</span><span style="font-weight: bold;">, </span><span style="color: #a31515;">"bold"</span><span style="font-weight: bold;">); </span></div>
<div style="margin: 0px;">
<span style="font-weight: bold;"> ... </span></div>
<div style="margin: 0px;">
<span style="font-weight: bold;">} </span></div>
</div>
<br />
Did you know that you could pass<span style="color: blue;"> </span><b style="color: blue;">data format string </b>as a parameter to the ToString() method?<br />
<br />
For example:<br />
<div style="background: white; color: black; font-family: Consolas; font-size: 11pt; padding-bottom: 8px; padding-left: 8px; padding-right: 8px; padding-top: 8px;">
<div style="margin: 0px;">
<span style="font-weight: bold;">e.Row.Cells[1].Text = iTotal.ToString(</span><span style="color: #a31515;">"F2"</span><span style="font-weight: bold;">)</span></div>
</div>
The above format string <a href="http://msdn.microsoft.com/en-us/library/system.web.ui.webcontrols.boundfield.dataformatstring.aspx">specifies </a>2 decimal places in a numeric value.Erik K.http://www.blogger.com/profile/04738667434492418561noreply@blogger.com0tag:blogger.com,1999:blog-6212515559541223786.post-84057622029924773362010-10-26T09:30:00.000-07:002012-06-12T10:01:36.305-07:00Simple Linq to Object example without CastingI found this discussion related to casting the result of a <b>LINQ </b>query to a static object at <a href="http://devlicio.us/blogs/derik_whittaker/archive/2008/02/22/simple-linq-to-object-example-with-casting.aspx">http://devlicio.us/blogs/derik_whittaker/archive/2008/02/22/simple-linq-to-object-example-with-casting.aspx</a> Since it was not possible to post a comment I decided to write a blog entry on my blog.<br />
<br />
The example given at the above page gives the following code:<br />
<br />
<div style="background: white; color: black; font-family: Consolas; font-size: 11pt;">
<br />
<div style="margin: 0px;">
<span style="color: #2b91af;">List</span><<span style="color: #2b91af;">Sport</span> ><span style="color: #2b91af;"> </span>sports = <span style="color: blue;">new</span> <span style="color: #2b91af;">List</span> <<span style="color: #2b91af;">Sport</span>>();</div>
<div style="margin: 0px;">
sports.Add(<span style="color: blue;">new</span> <span style="color: #2b91af;">Sport</span> { SportID = 1, Name = <span style="color: #a31515;">"Sport 1"</span>, Description = <span style="color: #a31515;">"Sport Desc 1"</span> });</div>
<div style="margin: 0px;">
sports.Add(<span style="color: blue;">new</span> <span style="color: #2b91af;">Sport</span> { SportID = 2, Name = <span style="color: #a31515;">"Sport 2"</span>, Description = <span style="color: #a31515;">"Sport Desc 2"</span> });</div>
<div style="margin: 0px;">
sports.Add(<span style="color: blue;">new</span> <span style="color: #2b91af;">Sport</span> { SportID = 3, Name = <span style="color: #a31515;">"Sport 3"</span>, Description = <span style="color: #a31515;">"Sport Desc 3"</span> });</div>
<div style="margin: 0px;">
sports.Add(<span style="color: blue;">new</span> <span style="color: #2b91af;">Sport</span> { SportID = 4, Name = <span style="color: #a31515;">"Sport 4"</span>, Description = <span style="color: #a31515;">"Sport Desc 4"</span> });</div>
<div style="margin: 0px;">
sports.Add(<span style="color: blue;">new</span> <span style="color: #2b91af;">Sport</span> { SportID = 5, Name = <span style="color: #a31515;">"Sport 5"</span>, Description = <span style="color: #a31515;">"Sport Desc 5"</span> });</div>
<div style="margin: 0px;">
<br /></div>
<div style="margin: 0px;">
<span style="color: blue;">var</span> query = <span style="color: blue;">from</span> s <span style="color: blue;">in</span> sports</div>
<div style="margin: 0px;">
<span style="color: blue;"> where</span> s.Name == <span style="color: #a31515;">"Sport 2"</span></div>
<div style="margin: 0px;">
<span style="color: blue;">select</span> s;</div>
<div style="margin: 0px;">
<br /></div>
<div style="margin: 0px;">
<div style="background: white; color: black; font-family: Consolas; font-size: 11pt;">
<div style="margin: 0px;">
<span style="color: #2b91af;">Sport</span> sport = (<span style="color: #2b91af;">Sport</span>)query.First();</div>
</div>
<br /></div>
</div>
<br />
I would like to note that you could rewrite the above statements using lambdas:<br />
<br />
<div style="background: white; color: black; font-family: Consolas; font-size: 11pt;">
<div style="margin: 0px;">
<span style="color: #2b91af;">Sport</span> sport = sports.Where(s => s.Name == <span style="color: #a31515;">"Sport 2"</span>).First();</div>
</div>
<br />
No "casting" is required.Erik K.http://www.blogger.com/profile/04738667434492418561noreply@blogger.com0tag:blogger.com,1999:blog-6212515559541223786.post-24965670230821887572010-08-03T08:06:00.000-07:002014-02-27T13:16:27.644-08:00Resize WinForm Controls AutomaticallyLet' say you have a form with a <b>ListBox </b>and OK and Cancel buttons.<br />
<br />
<div class="separator" style="clear: both; text-align: center;">
<a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEjKJXB6w3O9Fd1PmsreNpm0S6cWUiRdSqVv1u7TD2WHiM6ux2Whv7g4UDM0fcIgxmwrWqMy1OPJUH5SYVVcuYrnuPJRF12KrGwH__jOs0BSbD-tD4fxLerY75E_ICYlhp_SCHnvaEfYDRU/s1600/form_before.JPG" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"><img border="0" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEjKJXB6w3O9Fd1PmsreNpm0S6cWUiRdSqVv1u7TD2WHiM6ux2Whv7g4UDM0fcIgxmwrWqMy1OPJUH5SYVVcuYrnuPJRF12KrGwH__jOs0BSbD-tD4fxLerY75E_ICYlhp_SCHnvaEfYDRU/s320/form_before.JPG" /></a></div>
<br />
<a class="cssButton" href="javascript:void(0)" id="previewButton" target=""></a><br />
<br />
<br />
You want controls to be resized automatically when a form is resized. This is how you can achieve this by using the <b>Anchor</b> property:<br />
<br />
Place all controls inside a <b>GroupBox</b>. Set the <b>Anchor </b>and <b>Dock </b>properties<br />
<br />
GroupBox properties:<br />
<b>Anchor</b> : Top, Bottom, Left, Right<br />
<b>Dock</b> : None<br />
<br />
Buttons properties:<br />
<b>Anchor</b> : Bottom, Right<br />
<b>Dock </b>: None<br />
<br />
ListBox properties:<br />
<b>Anchor</b> : Top, Bottom, Left, Right<br />
<b>Dock </b>: NoneErik K.http://www.blogger.com/profile/04738667434492418561noreply@blogger.com0tag:blogger.com,1999:blog-6212515559541223786.post-46156197261395609312010-08-03T07:33:00.000-07:002012-06-12T10:03:02.000-07:00Save Window Position in RegistryThis is how you could save a Window position in the Registry so that the Window position could be restored next time the Form loads.<br />
<br />
<br />
Declare the following constant strings:<br />
<br />
<div style="margin: 0px;">
<span style="color: blue;"> public</span> <span style="color: blue;">const</span> <span style="color: blue;">string</span> REGKEY = @"SOFTWARE\MyCompany\MyApp\"; <span style="color: blue;"></span></div>
<div style="background: white; color: black; font-family: Consolas; font-size: 11pt; padding-bottom: 8px; padding-left: 8px; padding-right: 8px; padding-top: 8px;">
<div style="margin: 0px;">
<span style="color: blue;">public</span> <span style="color: blue;">const</span> <span style="color: blue;">string</span> WINWIDTH = <span style="color: #a31515;">"windowWidth"</span>;</div>
<div style="margin: 0px;">
<span style="color: blue;">public</span> <span style="color: blue;">const</span> <span style="color: blue;">string</span> WINHEIGHT = <span style="color: #a31515;">"windowHeight"</span>;</div>
</div>
<br />
Then, create a key for your application in the Registry. <br />
Declare a RegistryKey variable:<br />
private <span style="color: #2b91af;">RegistryKey</span> key;<br />
<br />
Use the following method to obtain the key: <br />
<br />
<div style="background: white; color: black; font-family: Consolas; font-size: 11pt;">
<div style="margin: 0px;">
<span style="color: blue;">public</span> <span style="color: #2b91af;">RegistryKey</span> GetKey(<span style="color: blue;">string</span> RegKey)</div>
<div style="margin: 0px;">
{</div>
<div style="margin: 0px;">
<span style="color: #2b91af;">RegistryKey</span> Key = <span style="color: #2b91af;">Registry</span>.CurrentUser.OpenSubKey(RegKey, <span style="color: blue;">true</span>);</div>
<div style="margin: 0px;">
<span style="color: blue;">if</span> (Key == <span style="color: blue;">null</span>)</div>
<div style="margin: 0px;">
{</div>
<div style="margin: 0px;">
Key = <span style="color: #2b91af;">Registry</span>.CurrentUser.CreateSubKey(RegKey);</div>
<div style="margin: 0px;">
}</div>
<div style="margin: 0px;">
<span style="color: blue;">return</span> Key;</div>
<div style="margin: 0px;">
}</div>
</div>
<br />
key = GetKey(REGKEY);<br />
<br />
Get window width and height:<br />
<br />
<div style="background: white; color: black; font-family: Consolas; font-size: 11pt; padding-bottom: 8px; padding-left: 8px; padding-right: 8px; padding-top: 8px;">
<div style="margin: 0px;">
<span style="color: blue;">int</span> width = Form1.Size.Width;</div>
<div style="margin: 0px;">
<span style="color: blue;">int</span> height = Form1.Size.Height;</div>
</div>
Save window width and height in the registry and close the RegistryKey.<br />
<br />
<br />
<div style="background: white; color: black; font-family: Consolas; font-size: 11pt; padding-bottom: 8px; padding-left: 8px; padding-right: 8px; padding-top: 8px;">
<div style="margin: 0px;">
key.SetValue(<span style="color: #2b91af;"></span>WINWIDTH, width);</div>
<div style="margin: 0px;">
key.SetValue(<span style="color: #2b91af;"></span>WINHEIGHT, height);</div>
<div style="margin: 0px;">
</div>
key.Close(); </div>
<div style="background: white; color: black; font-family: Consolas; font-size: 11pt; padding-bottom: 8px; padding-left: 8px; padding-right: 8px; padding-top: 8px;">
<div style="margin: 0px;">
</div>
<div style="margin: 0px;">
When you load your form, read the values from the registry:</div>
<div style="margin: 0px;">
</div>
<div style="background: white; color: black; font-family: Consolas; font-size: 11pt; padding-bottom: 8px; padding-left: 8px; padding-right: 8px; padding-top: 8px;">
<div style="margin: 0px;">
<span style="color: #2b91af;">width = Convert</span>.ToInt16 (key.GetValue(WINWIDTH));</div>
<div style="margin: 0px;">
<span style="color: #2b91af;">height = Convert</span>.ToInt16 (key.GetValue(WINHEIGHT));</div>
<br />
Set the new window size:</div>
<div style="margin: 0px;">
<div style="margin: 0px;">
Form1.Size = <span style="color: blue;">new</span> <span style="color: #2b91af;">Size</span>(width, height);</div>
</div>
<div style="margin: 0px;">
</div>
<div style="margin: 0px;">
</div>
</div>Erik K.http://www.blogger.com/profile/04738667434492418561noreply@blogger.com0tag:blogger.com,1999:blog-6212515559541223786.post-53290609715530146852010-07-20T13:31:00.000-07:002012-06-12T10:22:56.177-07:00Concise Code to Enable/Disable WinForm ControlsLet's say you have a check box (cb1) on a form that enables a text box (txt1) and disables a listbox (lst1). You can write a statement that shows this dependency on 2 lines:<br />
<br />
<span style="color: blue;">txt1.Enabled = cb1.Checked;</span><br />
<div style="color: blue;">
lst1.Enabled = !cb1.Checked;</div>
<br />
This works fine. However, we repeat the reference to the check box state twice.<br />
There is a way to write the above statements on the same line:<br />
<br />
<span style="color: blue;">lst1.Enabled = !(txt1.Enabled = cb1.Checked);</span><br />
<br />
Looks simple when you get it right. But it took me a few minutes to figure it out.Erik K.http://www.blogger.com/profile/04738667434492418561noreply@blogger.com0tag:blogger.com,1999:blog-6212515559541223786.post-60291756272674158522010-07-16T13:30:00.000-07:002012-06-12T10:20:48.669-07:00Examine Columns in ASP.NET Dynamic Data MetaTableThis is how you get access to all columns in a <b>MetaTable </b>that serves as a data source to your controls like a <b>DetailsView </b>or a <b>GridView</b>. <b>Dynamic Data</b> generates the following code for each <b>ASP.NET</b> page by default:<br />
<br />
<br />
<div style="background: white; color: black; font-family: Consolas; font-size: 11pt;">
<div style="margin: 0px;">
<span style="color: blue;">protected</span> <span style="color: #2b91af;">MetaTable</span> table;</div>
<div style="margin: 0px;">
<br /></div>
<div style="margin: 0px;">
<span style="color: blue;">protected</span> <span style="color: blue;">void</span> Page_Load(<span style="color: blue;">object</span> sender, <span style="color: #2b91af;">EventArgs</span> e)</div>
<div style="margin: 0px;">
{</div>
<div style="margin: 0px;">
table = DetailsDataSource.GetTable();</div>
<div style="margin: 0px;">
Title = table.DisplayName; </div>
<div style="margin: 0px;">
}</div>
</div>
The <b>MetaTable </b>class has a <b>Columns </b>property that returns a collection of <b>MetaColumns </b>for the table. This allows us to enumerate through the collection and check for any custom attributes on table columns:<br />
<div style="background: white; color: black; font-family: Consolas; font-size: 11pt;">
<div style="margin: 0px;">
<span style="color: blue;"> </span></div>
<div style="margin: 0px;">
<span style="color: blue;"> foreach</span> (<span style="color: #2b91af;">MetaColumn</span> column <span style="color: blue;">in</span> table.Columns) </div>
<div style="margin: 0px;">
{</div>
<div style="margin: 0px;">
<div style="background: white; color: black; font-family: Consolas; font-size: 11pt;">
<div style="margin: 0px;">
<span style="color: blue;"> if</span> (column.Attributes.OfType<<span style="color: #2b91af;">MyCustomAttribute</span>>().Count() > 0)</div>
</div>
</div>
<div style="margin: 0px;">
......</div>
<div style="margin: 0px;">
}</div>
<div style="margin: 0px;">
} </div>
</div>
<br />
This is based on a tip found at <a href="http://weblogs.asp.net/andrewrea/archive/2009/03/25/asp-net-dynamic-data-using-column-generators-based-on-meta-data.aspx">this </a>blog.Erik K.http://www.blogger.com/profile/04738667434492418561noreply@blogger.com0tag:blogger.com,1999:blog-6212515559541223786.post-31798816803431724692010-07-08T10:39:00.000-07:002012-02-14T08:26:51.839-08:00Reverse .NET SortedList<div style="background: none repeat scroll 0% 0% white; color: black; font-family: Consolas; font-size: 11pt; padding: 8px;"><div style="margin: 0px;"> This post shows how you could reverse a <b>SortedList </b>in C# by its keys:</div><div style="background: none repeat scroll 0% 0% white; color: black; font-family: Consolas; font-size: 11pt; padding: 8px;"><div style="background: none repeat scroll 0% 0% white; color: black; font-family: Consolas; font-size: 11pt; padding: 8px;"><div style="margin: 0px;"><br />
</div><div style="margin: 0px;"><span style="color: #2b91af;">SortedList</span><<span style="color: blue;">string</span>, <span style="color: blue;">string</span>> strlist = <span style="color: blue;">new</span> <span style="color: #2b91af;">SortedList</span><<span style="color: blue;">string</span>, <span style="color: blue;">string</span>>();</div><div style="margin: 0px;">strlist.Add(<span style="color: #a31515;">"f3"</span>, <span style="color: #a31515;">"xxx"</span>);</div><div style="margin: 0px;">strlist.Add(<span style="color: #a31515;">"f1"</span>, <span style="color: #a31515;">"ccc"</span>);</div><div style="margin: 0px;">strlist.Add(<span style="color: #a31515;">"f2"</span>, <span style="color: #a31515;">"aaa"</span>);</div><div style="margin: 0px;"><br />
</div><div style="margin: 0px;"><span style="color: blue;">foreach</span> (<span style="color: #2b91af;">KeyValuePair</span><<span style="color: blue;">string</span>, <span style="color: blue;">string</span>> kvp <span style="color: blue;">in</span> strlist)</div><div style="margin: 0px;">{</div><div style="margin: 0px;"> <span style="color: #2b91af;">Console</span>.WriteLine(<span style="color: #a31515;">"Key = {0}, Value = {1}"</span>, kvp.Key, kvp.Value);</div><div style="margin: 0px;">} </div><div style="margin: 0px;"><br />
</div><div style="margin: 0px;"><span style="color: #2b91af;">Console</span>.WriteLine(<span style="color: #a31515;">"reverse the list"</span>);</div><div style="margin: 0px;"><span style="color: #2b91af;">IEnumerable</span><<span style="color: #2b91af;">KeyValuePair</span><<span style="color: blue;">string</span>, <span style="color: blue;">string</span>>> revList = strlist.Reverse();</div><div style="margin: 0px;"><span style="color: blue;">foreach</span> (<span style="color: #2b91af;">KeyValuePair</span><<span style="color: blue;">string</span>, <span style="color: blue;">string</span>> kvp <span style="color: blue;">in</span> revList)</div><div style="margin: 0px;">{</div><div style="margin: 0px;"> <span style="color: #2b91af;">Console</span>.WriteLine(<span style="color: #a31515;">"Key = {0}, Value = {1}"</span>, kvp.Key, kvp.Value);</div><div style="margin: 0px;">}</div><br />
<div style='float:left; margin-right:4px;'><script type="text/javascript"><!--
google_ad_client = "ca-pub-8504512626105280";
/* 300x250, created 12/9/10 */
google_ad_slot = "7115539031";
google_ad_width = 300;
google_ad_height = 250;
//-->
</script><br />
<script type="text/javascript"
src="http://pagead2.googlesyndication.com/pagead/show_ads.js">
</script><br />
</div><br />
</div></div></div>Erik K.http://www.blogger.com/profile/04738667434492418561noreply@blogger.com0