Derik Whittaker

Syndication

News


Images in this post missing? We recently lost them in a site migration. We're working to restore these as you read this. Should you need an image in an emergency, please contact us at imagehelp@codebetter.com
Anonymous Casting with Linq to Sql

***** Disclaimer ******
I may be the last person to figure this out, but oh well.
***** End Disclaimer ******

Today as I was further exploring the coolness that is Linq to Sql I ran into a little situation.  I was creating a query where I wanted to use the columns form multiple tables to build an object.  Since the results did not map 1 to 1 from a table to a object I figured I would have to do it the mapping the hard way.  Here was the code I originally had.

var results = from c in DBContextInstance().Companies
              join pc in DBContextInstance().ProgramControls on c.CoID equals pc.CoID
              select new {c.CoID, c.CoDesc, pc.ProjectNotes, pc.ProjectManager};

List newList = new List< HybridObject >();

foreach ( var result in results )
{
    HybridObject entity = new HybridObject
                              {
                                  CompanyID = result.CoID,
                                  CompanyDesc = result.CoDesc,
                                  ProjectNotes = result.ProjectNotes,
                                  ProjectManager = result.ProjectManager
                              };
    newList.Add( entity  );
}

After looking at the code above for a few minutes, I really, really did not like it.  I knew there had to be a better way.  So I started playing around with various ways to do this.  I had a hunch that since I was creating a return value as an Anonymous type, I should be able to create a concrete type in its place.  Sure enough, I could.  With a little help from constructor initializers (sure you could also do this by creating a constructor that took in the values as well).  Take a look at the new, cleaner code below.

var results = ( from c in DBContextInstance().Companies
              join pc in DBContextInstance().ProgramControls on c.CoID equals pc.CoID
              select new HybridObject
                         {
                                CompanyID = CoID,
                                CompanyDesc = CoDesc,
                                ProjectNotes = ProjectNotes,
                                ProjectManager = ProjectManager
                         } ).ToList();

Now if you ask me, the new code is much more concise and much simpler.  It also does not waste CPU cycles, which is always a plus.

If anyone has a better way, please let me know.

Till next time,


Posted 04-09-2008 5:39 PM by Derik Whittaker
Filed under: ,

[Advertisement]

Comments

DotNetKicks.com wrote Anonymous Casting with Linq to Sql
on 04-09-2008 6:40 PM

You've been kicked (a good thing) - Trackback from DotNetKicks.com

Links Today (2008-04-10) wrote Links Today (2008-04-10)
on 04-10-2008 11:32 AM

Pingback from  Links Today (2008-04-10)

Ben Scheirman wrote re: Anonymous Casting with Linq to Sql
on 04-11-2008 1:52 PM

Yeah that was pretty obvious to me while reading your post...  *jab* ;) !

At least you're posting your experiences though, it is helpful to people learning this stuff.

Derik Whittaker wrote re: Anonymous Casting with Linq to Sql
on 04-11-2008 1:58 PM

@Ben,

That hurts....  I just may cry now.

Zolpidem ambien. wrote Zolpidem.
on 08-25-2008 3:03 PM

Cheap zolpidem persriptions. Zolpidem eszopiclone indications. Zolpidem overdose. Zolpidem.

About The CodeBetter.Com Blog Network
CodeBetter.Com FAQ

Our Mission

Advertisers should contact Brendan

Subscribe
Google Reader or Homepage

del.icio.us CodeBetter.com Latest Items
Add to My Yahoo!
Subscribe with Bloglines
Subscribe in NewsGator Online
Subscribe with myFeedster
Add to My AOL
Furl CodeBetter.com Latest Items
Subscribe in Rojo

Member Projects
DimeCasts.Net - Derik Whittaker

Friends of Devlicio.us
Red-Gate Tools For SQL and .NET

NDepend

SlickEdit
 
SmartInspect .NET Logging
NGEDIT: ViEmu and Codekana
LiteAccounting.Com
DevExpress
Fixx
NHibernate Profiler
Unfuddle
Balsamiq Mockups
Scrumy
JetBrains - ReSharper
Umbraco
NServiceBus
RavenDb
Web Sequence Diagrams
Ducksboard<-- NEW Friend!

 



Site Copyright © 2007 CodeBetter.Com
Content Copyright Individual Bloggers

 

Community Server (Commercial Edition)