NHibernate.Linq Pitfalls: Casting(notherdev.blogspot.com)

submitted by notherdevnotherdev(238) 6 months, 26 days ago

One thing that needs to be always remembered when writing NHibernate.Linq queries is that it is going to be translated into SQL eventually. What this means is that we can't do everything in our Select or Where conditions - we are restricted by the capabilities of underlying database and SQL language itself.

7 comments |category: |Views: 123

tags: another

new Add a live kick counter to your blog >> liveImage

You can even customize the image by choosing your own colors, and then clicking the button below to update the preview and the html code:

  • "Kick It" text
  • "Kick It" background
  • kick count text
  • kick count background
  • border

Simply copy and paste this HTML into your blog post.


Users who kicked this story:
Comments:

posted by dpetersondpeterson(4397) 6 months, 24 days ago 0

This is one of those spots where I feel Linq isn't as good as other solutions I've seen. In Sybase's PowerBuilder product, you can pass an array directly to a SQL IN statement and it will automatically populate the IN statement. It would be much cleaner if Linq could do something similar here.

Reply

posted by notherdevnotherdev(238) replied to dpetersondpeterson(4397), 6 months, 24 days ago 0

You can do that in NHibernate.Linq too - my second query does that. But you need to ensure that the types are converted correctly and at C#-side.

Reply

posted by dpetersondpeterson(4397) replied to notherdevnotherdev(238), 6 months, 24 days ago 0

Ah, it didn't seem like that was happening based on your code. Does the SQL generated by that code use an IN statement?

Reply

posted by notherdevnotherdev(238) replied to dpetersondpeterson(4397), 6 months, 24 days ago 0

Yes, it does.

Reply

posted by dpetersondpeterson(4397) replied to notherdevnotherdev(238), 6 months, 24 days ago 0

My mistake then, looking at the code it seemed like Linq would do the filtering after the results were returned. Seems it's smarter than I guessed!

Reply

posted by bradygasterbradygaster(4897) 6 months, 24 days ago 0

Using NHProfiler for things debugging your NH code makes things like this a little easier to find. Everyone gets to learn these gems of the art of NHibernate through experiencing the pain first-hand. It's nice to see example articles like this pop up to explain those ins and outs.

Reply

posted by NoldorinNoldorin(829) 6 months, 23 days ago 0

This is an important pitfall to point out, so well done for that.

However, I believe I'm right in saying that an even easier way, and certainly more efficient (it doesn't require the conversion to a list and the corresponding allocation of memory) is to call `.AsEnumerable()` on the enumerable *after* the local operations but *before* the SQL operations.

Reply

information Login or create an account to comment on this story