I are tupid

This may be novel or really dumb, but I like it. Say you want to convert a Dictionary to a List of KeyValuePairs that are sorted by something within the dictionary Key or Value. Don’t ask why, just go with it. You could do this:

Where someDictionary is Dictionary<Type, string> :

List<KeyValuePair<Type, String>> dataSource = new List<KeyValuePair<Type, String>>(someDictionary);
dataSource.Sort(new SomeComparer<KeyValuePair<Type, String>>("Value", true));

To:

var query = from keyValuePair in someDictionary
          orderby keyValuePair.Value
          select new KeyValuePair<Type, String>(keyValuePair.Key, keyValuePair.Value);
SomeMethod(query.ToList());

If nothing else, you don’t have to create or implement a System.Collections.IComparer class to use the .Sort method. That seems worth it. That and I think it just plain looks better, but that just me. If I am completely wrong here, refer to the title of this post. Just a thought really.

Using the ForEach method on List Collections


public static StateCollection GetByName(String partialName)
{
 StateCollection returnValue;

 returnValue = new StateCollection();

  //baseList is some list I wasn't nice enough to show where it came from.
  //It's just a list of states.  Get over it.
 var stateList = from state in baseList
                 where state.Name.StartsWith(partialName)
                 select new State(state.Name, state.Code);

 stateList.ToList().ForEach(currentState => returnValue.Add(currentState));

 return returnValue;
}

So what is done here:

Basically I am using a linq expression to get states from a list of states (Like Michigan, Illinois, or Canada) based on name. No big deal. Then I take the query and produce a List from it. AMAZIN!


stateList.ToList().ForEach(curretState => returnValue.Add(curretState));

ForEach is an extension method for List<>. Now if you remember from previous posts, that means it resides in a static class somewhere that “tacks” it on to the List<> class. Basically this method acts like a typical foreach loop. (You know, go through the list and do something. Or nothing. I suppose you could just loop through if you wanted to. I won’t tell you how to live your life.) Simple but in my opinion, much cleaner looking.

I mean I could do this:


public static StateCollection GetByName(String partialName)
{
 StateCollection returnValue;

 returnValue = new StateCollection();

 foreach(State currentState in baseList)
 {
   if(currentState.Name.StartsWith(partialName)
   {
     returnValue.Add(currentState);
   }
 }

 return returnValue;
}

Really it’s just up to what you prefer. (And I’m sure you could drive a car with no power brakes. You’ll still get there.) Also, I really didn’t need the linq expression since I could have done this all with ForEach (Provided baseList is IEnumerable).

One last note, all IEnumerable collections have the ToList() method (And a bunch of other ones for that matter.)

Filling a list of classes with random values using linq

Filling a list of classes (10 for this example) with random values using linq.

Enumerable.Range(0, 10)
– This means give me a list from 1 to 10

Enumerable.Range(0, 10).Select
(
  j =>

– This means you are going to select every j in the list Enumerable created

Enumerable.Range(0, 10).Select
(
  j => new JunkItemB()

– For every J in the list, create a new junkItem

Enumerable.Range(0, 10).Select
(
  j => new JunkItemB()
  {
    SomeNumber = Enumerable.Range(0, 1).Select
    (
      i => randomGenerator.Next()
    ).ElementAt(0)
  }
)

– This is the new way to set a property in 3.5. I could just use a constructor, and probably should but this was just thrown together.

Enumerable.Range(0, 10).Select
(
  j => new JunkItemB()
  {
    SomeNumber = Enumerable.Range(0, 1).Select
    (
      i => randomGenerator.Next()
    ).ElementAt(0)
  }
)

– Once again, I am using Enumerable to create a list. This one will have 1 item. I will use Select to go through the list.For every i in the list, test (this is the Random object) will create a random number and give me a list of random numbers the size of the Enumerable.Range list.

Enumerable.Range(0, 10).Select
(
  j => new JunkItemB()
  {
    SomeNumber = Enumerable.Range(0, 1).Select
    (
      i => randomGenerator.Next()
    ).ElementAt(0)
 }
).ToList()

– I just want one random number, so I will get the number t the first index. Although with this, I only have on number in the list anyhow. The property of the current JunkItemB will be set to this. Once this is done, it will be repeated 9 more times.

Enumerable.Range(0, 10).Select
(
  j => new JunkItemB()
  {
    SomeNumber = Enumerable.Range(0, 1).Select
    (
      i => randomGenerator.Next()
    ).ElementAt(0)
 }
).ToList()
- Now that I have my list of 10 JunkItemBs, I will create a new list and from it giving me 10 JunkItemBs with preset SomeNumber properties.

Now that I look at this again (This is taken from notes I had somewhere else) I could have just used Random to get a number instead of Enumerator. Yay for over complicating things. Also my explanation of Select is a bit misleading for those who haven’t used lambda expressions. I’ll explain further in my next post.

Namespaces:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;