Linq Casting: ToDictionary()

In this post I will give an example how to cast a GenericList to Dictionary.

This example will use the following Blogger class.


public class Blogger
{
 public string FirstName { get; set; }
 public string LastName { get; set; }
 public int Age { get; set; }
 public string Blog { get; set; }
}


The example will cast a List<Blogger> to a dictionary with key FirstName, Lastname and value the Age of the blogger.


// DECLARE PERSONLIST

List personList = new List();
personList.Add(new Blogger { FirstName = "Pieter", LastName = "Brinkman", Age = 27, Blog = "http://blog.newguid.net" });
personList.Add(new Blogger { FirstName = "Mark", LastName = "van Aalst", Age = 26, Blog = "http://www.markvanaalst.com/" });
personList.Add(new Blogger { FirstName = "Bas", LastName = "Hammendorp", Age = 32, Blog = "http://www.hammendorp.net/" });

// CREATE NEW DICTIONARY FROM LIST
// with key FirstName + LastName and value Age

Dictionary AgeDictionary =
 personList.ToDictionary(x =&gt; x.FirstName + " " + x.LastName,
       x =&gt; x.Age,
       StringComparer.OrdinalIgnoreCase);

// GENERATE OUTPUT

foreach (KeyValuePair item in AgeDictionary)
 Response.Write("key: " + item.Key + " - value: " + item.Value + "<br />");

//// OUTPUT
//key: Pieter Brinkman - value: 27
//key: Mark van Aalst - value: 26
//key: Bas Hammendorp - value: 32


 Hope it helps.

Using the ViewState within the SelectMethod of a ObjectDataSource

By default it is not possible to use the ViewState within methods of a ObjectDataSource. Because the DataSource doesn’t run within the current page instance, it just fires the method that you specified.

You can set the page instance for the DataSourceby setting the ObjectInstance property in the ObjectCreating event of the DataSource.


&lt;asp:ObjectDataSource ID=&quot;odsListing&quot; runat=&quot;server&quot; SelectMethod=&quot;getItems&quot; TypeName=&quot;YOUR.NAMESPACE&quot; OnObjectCreating=&quot;ObjectDataSource_ObjectCreating&quot; DataObjectTypeName=&quot;List"&gt;



protected void ObjectDataSource_ObjectCreating(object sender, ObjectDataSourceEventArgs e)
{
 e.ObjectInstance = this;
}


Now you can use the ViewState in you DataSourceMethod (getItems() in this example).

WCF webservice error with DBML objects

When deploying a WCF webservice for a Silverligh application I got the following error:


An ExceptionDetail, likely created by IncludeExceptionDetailInFaults=true, whose value is:
System.InvalidOperationException: An exception was thrown in a call to a WSDL export extension: System.ServiceModel.Description.DataContractSerializerOperationBehavior
 contract: <a href="http://tempuri.org/:IWebService">http://tempuri.org/:IWebService</a> ----&gt; System.Runtime.Serialization.InvalidDataContractException: Type 'Project.service.HU_BACH.ScPlacemark' cannot be serialized. Consider marking it with the DataContractAttribute attribute, and marking all of its members you want serialized with the DataMemberAttribute attribute.


As you can see from the error you need to add the [Serializable] attribute to all objects that are used within the webservice. After I did this I got the same error for my Linq to Sql objects generated within my DBML. You can make your Linq to Sql objects serializable by changing the dbml setting Serialization Mode to Unidirectional.

 
Hope it helps.

Asp.Net: Clear inputfields after form submit

Every time a form is successful summited I need to clear all input-fields to the default values and give feedback to the user. I wrote a method ClearControl that can have a control as input parameter. This method will set the control based on his type back to the defaultvalue.


 public static void ClearControl(Control control)
{
 switch (control.GetType().Name)
 {
  case "TextBox":
   TextBox txtBox = (TextBox)control;
   txtBox.Text = "";
   break;
  case "DropDownList":
   DropDownList ddl = (DropDownList)control;
   ddl.SelectedIndex = 0;
   break;
  case "CheckBox":
   CheckBox chk = (CheckBox)control;
   chk.Checked = false;
   break;
  case "CheckBoxList":
   CheckBoxList chkList = (CheckBoxList)control;
   foreach (ListItem li in chkList.Items)
    li.Selected = false;
   break;
  case "Panel":
   ClearFields((Panel)control);
   break;
  case "RadioButtonList":
   RadioButtonList rbl = (RadioButtonList)control;
   rbl.SelectedIndex = -1;
   break;
 }
}


To use this method I wrote a method ‘ClearFields’ that accepts a View or Container control. You can add every type of control that has the .Controls property.


public static void ClearFields(Panel container)
{
 foreach (Control control in container.Controls)
 {
  ClearControl(control);
 }
}

public static void ClearFields(View container)
{
 foreach (Control control in container.Controls)
 {
  ClearControl(control);
 }
}


When I have some time left I will try to implement the ClearFields Method as a ExtensionMethod.

Cheers,

Pieter

Asp.net: Sort generic list with a delegate.

You can use a delegate to sort a generic list.

Example

 

GenericList ObjectItem = new GenericList ObjectItem()

GenericList.Sort(delegate(ObjectItem i1,ObjectItem i2)
{
 return i1.SortProperty.CompareTo(i2.SortProperty);
});