|
Querying some data where each item may or may not have the same set of fields really throws off the WPF DataGrid . As it binds each row it throws and catches an exception every time it can't find a field entry. This is SLOOOOOOOW for a sparsley normal (if that's a term) data set.
Normalizing the data get's around all these possible exceptions at the expensive of determining the union of all fields and iterating over all of the data filling in the gaps.
public static IEnumerable<IDictionary<TKey, TValue>> Normalize<TKey, TValue>(
this IEnumerable<IDictionary<TKey, TValue>> rows)
{
return rows.Normalize<TKey, TValue>(EqualityComparer<TKey>.Default);
}
public static IEnumerable<IDictionary<TKey, TValue>> Normalize<TKey, TValue>(
this IEnumerable<IDictionary<TKey, TValue>> rows,
IEqualityComparer<TKey> comparer)
{
Debug.Assert(rows != null);
IEnumerable<TKey> columns = rows.SelectMany(d => d.Keys).Distinct(comparer);
List<IDictionary<TKey, TValue>> list = new List<IDictionary<TKey, TValue>>();
foreach (IDictionary<TKey, TValue> row in rows)
{
foreach (TKey column in columns.Where(column => row.ContainsKey(column) == false))
row.Add(column, default(TValue));
list.Add(row);
}
return list;
}
10 PRINT Software is hard. - D Knuth
20 GOTO 10
|
|
|
|
|
Writing this little extension method brought back some memories of VARIANT and IDispatch(Ex) ...
public static void SetValue(this object o, PropertyInfo property, object value)
{
Debug.Assert(o != null);
if (property != null)
property.SetValue(o, Convert.ChangeType(value, property.PropertyType), null);
}
Sure is easier to be duck typed nowadays in Microsoft-land.
10 PRINT Software is hard. - D Knuth
20 GOTO 10
|
|
|
|
|
Here's a nice little addition to .NET 4.0: formalization of the lazy loader pattern. A simple looking generic that takes a lamba function in its constructor that will get called on the first access to the Value property.
Take this code:
private CommandCollectionViewModel _menuCommands;
public CommandCollectionViewModel MenuCommands
{
get
{
if(_menuCommands == null)
_menuCommands = CreateCommands();
return _menuCommands;
}
} and replace it with this
private Lazy<CommandCollectionViewModel> _menuCommands;
public MainWindowViewModel()
{
_menuCommands = new Lazy<CommandCollectionViewModel>(() => CreateCommands());
]
public CommandCollectionViewModel MenuCommands
{
get
{
return _menuCommands.Value;
}
} Cleans up the getters and puts member initialization in the constructor; which is a pattern I prefer.
10 PRINT Software is hard. - D Knuth
20 GOTO 10
modified on Monday, November 30, 2009 5:30 PM
|
|
|
|
|
In my ongoing quest of not becoming totally useless as a software engineering manager I'm finally starting up the WPF hill.
Dang that sucker's steep. Can't I just have my win32 back?
10 PRINT Software is hard. - D Knuth
20 GOTO 10
|
|
|
|
|
Here's a handy method if you're dealing with a lot of Nullable types.
public static IEnumerable<T> Coalesce<T>(this IEnumerable<T?> source) where T : struct
{
Debug.Assert(source != null);
return source.Where(x => x.HasValue).Select(x => (T)x);
}
(I looked around for some built in Linq method to do this but didn't find one.)
10 PRINT Software is hard. - D Knuth
20 GOTO 10
modified on Wednesday, November 18, 2009 2:51 PM
|
|
|
|
|
At first I thought extension methods were kind of silly. Nothing but static methods and compiler tricks.
And while extension methods are indeed nothing but smoke and mirrors around static methods, I have grown to like them.
In the modern world of "Intellisense driven development" they help a ton. Cause I will say this much: without Intellisense I wouldn't have had a chance in heck of figuring Linq out.
10 PRINT Software is hard. - D Knuth
20 GOTO 10
modified on Saturday, January 30, 2010 9:31 AM
|
|
|
|
|
The iPhone-like shell for WindowsMobile is really starting to come together.
Starting from Bertoneri Luigi's code (posted here on CodeProject: http://www.codeproject.com/KB/mobile/IPhoneUI.aspx[^]) it's now got a bunch of new stuff:
Per Pixel alpha blending, integration with WindowsModbile SystemState changes (for notification of things like missed calls etc), integration with Windows Mobile Start menu links, plus some other little bells and whistles.
What's been the most fun is making a declarative framework so that most of the UI gets stitched together with XML. Next I think I'm going to see if all of that can be done or redone in SilverLight Mobile.
If you're curious or would like to help with some coding check it out at:
http://www.codeplex.com/iPhoneUI[^]
10 PRINT Software is hard. - D Knuth
20 GOTO 10
|
|
|
|