Linq Casting: ToDictionary()

by Pieter Brinkman 23. June 2009 04:03

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

This example will use the following Blogger class.

[code:c#]

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

[/code]

 

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

[code:c#]

// DECLARE PERSONLIST

List<Blogger> personList = new List<Blogger>();
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<string, int> AgeDictionary =
 personList.ToDictionary(x => x.FirstName + " " + x.LastName,
       x => x.Age,
       StringComparer.OrdinalIgnoreCase);


// GENERATE OUTPUT

foreach (KeyValuePair<string, int> 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

[/code]

 Hope it helps.

Categories: Linq

Playing with JQuery (fixing Intellisense in VS2008)

by Pieter Brinkman 23. June 2009 03:49

The last few years I spend a lot of time working with Asp.Net AJAX. It all worked pretty good, the only downside is that you do not have control the generated HTML. With jQuery you can manipulate generated HTML. This HTML can be generated fully controlled with a ListView.

I'm a lazy programmer so the first thing I needed to do is fix jQuery Intellisense in VS2008. I found my information for doing this on Scott Gu's blog (jQuery Intellisense in Vs 2008). Everything worked great the only bad thing is that my computer needed a restart after installing a Visual Studio Patch...

Now the Intellisense is working the only thing I need to do is blog some nice examples of my work! ;-)

Cheers,

Pieter

 

Set CultureCode for RDLC (SQL reporting report)

by Pieter Brinkman 11. June 2009 06:34

You can set the culture of a report by clicking the report and setting the Language property. If you want to support multiple cultures you can set the property to =User!Language .

 

Categories: ASP.Net

Using the ViewState within the SelectMethod of a ObjectDataSource

by Pieter Brinkman 3. June 2009 07:32

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.

[code:html]

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

[/code]

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

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

 

Categories: ASP.Net | Controls