Random programming things I'd want to remember

Wednesday, September 18, 2013

Efficient way to save lists to IsolatedStorage

I am working on a Windows 8 project and I needed a long list of strings in the isolated storage. I used this code:
for(int i=0;i<myList.Count;i++)
  await FileIO.AppendTextAsync(myFileName, myList[i]);

The method is of course marked as async, so when my debugger skipped over that line and nothing happened, I though something was wrong with Visual Studio. Then I set the debugger at my AppendTextAsync line and when it stopped, I realized that it was taking forever for the computer to append the items from my list, one-by-one (there were more than ten thousand items, and my computer is not the slowest one out there.

I recently read the book by Marijn Haverbeke called Eloquent JavaScript, which has a pretty heavy (well, in my mind) emphasis on functional programming. I remembered that there is a Join method that I could use to convert my list into a string, and I did it like so:

string result = string.Join(Environment.NewLine, myList.ToArray());
and voila! It took a split second.

Windows Powershell: two commands to rule the all! Well, almost all...

I borrowed a book on Powershell from Amazon, it was short and easy to read, but I only remember two commands from the book: Get-Command and Get-Help. Get-Command lists all available commands and Get-Help can tell everything about that command. If I know that I need to get something, but not sure about the command itself, I can type the following into my PowerShell window: "Get-Command -Verb Get". And if I forget how to use Get-Command, I can always type "Get-Help Get-Command". So I only need to remember one command, Get-Help!

Thursday, September 12, 2013

Entity Framework ignores some fields when pulling data

Let's say I have a class:
public class Bookshelf
    public int BookshelfID {get;set;}
    public List<string>BookNames {get;set;}

and it corresponds to a table named Bookshelves
CREATE TABLE [dbo].[Bookshelves] (
     BookshelfID INT IDENTITY (1,1) NOT NULL
   , BookNames varchar(MAX)...

in the database. Let's say we are using Entity Framework (4.0), like so:
public class EFDbContext : DbContext
    public DbSet<Bookshelf> Bookshelves {get;set;}

When Entity Framework generates the SELECT statement to pull all data from table Books, the resulting SQL will be the following:
    SELECT BookshelfID FROM Bookshelves

totally ignoring the BookNames property. But if you alter the class definition such that the BookNames property is a string, the SQL statement will include BookNames column. I guess Entity Framework only works with basic types and does not trust me with explaining it how to treat the lists. You can use LINQ to Objects to extract whatever you need from BookNames.

The bottom line is: if Entity Framework is ignoring some columns in the table, check two things:
  1. Your class contains the corresponding property, and
  2. The datatype is one of these (MSDN article) primitive types.

Sunday, September 8, 2013

Separator array in place for string.Split function

Here is how to use the .Split function without having to define an array of separator values separately.

.Split(new char[]{';'}, StringSplitOptions.RemoveEmptyEntries)