Home > C#, Extension Methods, LINQ > L2S Extensions…MVC style

L2S Extensions…MVC style

Four years later…I’ve finally come up for breath.  I hope to start a better pattern of trying to get some technical posts created once in a while.  Until I have my first inspiration – that I feel would be beneficial to anyone in the WWW, I thought I’d provide a little update to my L2S Extensions.

Here is what has changed since my last post 4 years ago.  I’m going to make the assumption that since there was really only three new improvements, that the code most have been pretty solid Smile.  Some of the changes below were due to the fact that the main code I generate for my company has shifted to the ASP.NET MVC framework and have started leveraging the System.ComponentModel.DataAnnotations namespace to decorate pretty much everything.  Since I was querying some of those same classes, it made sense to update my L2S Extensions code to obey some attributes as well.  All the changes below occur in DumpCSV().

  1. Control what is exported by leveraging the ScaffoldColumn attribute.
  2. Exports all fields and properties of your IQueryable object.  Before, if the individual entities had properties, it only tried to export those and ignored fields.  (Thanks to a reader Jason D for the code change).
  3. Only fields/properties whose memberType satisfies the following will be exported:
    new [] { typeof( string ), typeof( ValueType ) }
                .Any( t => t.IsAssignableFrom( memberType ) ) )
  4. When determining column headers for output, it looks for a Display attribute and leverages that if available.
  5. DumpCSV() has an override that takes a delegate for translating values.  This is useful, if your CSV dump should contain labels associated with the ‘keys’ stored in the database.
  6. Finally, it was updated and compiled against .NET 4.0.  No code changes were really needed, so if you aren’t on that framework yet, I’d think you could just add the class files to a 2.0 and greater framework project and work just fine.
    Here is an example of the Translate delegate I’ve used in some of my scripts.
[TypeMapping( typeof( Lookups ), "StateValue", "Key", "StateText" )]
[Display( Name = "State" )]
public virtual State? State { get; set; }

public object TranslateValue( MemberInfo m, object row )
    var p = m as PropertyInfo;
    var f = m as FieldInfo;
    var lookupAttr = m.GetAttribute<TypeMappingAttribute>();
    if ( p != null )
        return lookupAttr != null
            ? Expression
                .Lambda( Expression.Constant( p.GetValue( row, null ) )
                .ConvertToData( lookupAttr ) )
            : p.GetValue( row, null  );
        return lookupAttr != null
            ? Expression
                .Lambda( Expression.Constant( f.GetValue( row ) )
                .ConvertToData( lookupAttr ) )
            : f.GetValue( row );
      So this example looks a little crazy.  But essentially, we had a custom attribute (


      ) that would associate a field with an Enum.  This attribute then had the information needed to get the text display (


    in this case).  So whatever your need might be when translating a value from the actual database, if it can’t be accomplished directly with a L2S query, this override gives you the chance to massage your data before dumping it to the *.csv file.

As usual, the code can be found here.

Categories: C#, Extension Methods, LINQ
  1. April 29, 2014 at 10:15 am
  1. No trackbacks yet.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )


Connecting to %s

%d bloggers like this: