views:

14

answers:

1

I need to change the value of a property when I query the database using EF4. I have a company code that gets returned and I need to translate it to another company code, if needed. So, there is a stored procedure that is used to do this currently. Here's the old select statement.

SELECT companyName, TranslateCompanyCode(companyCode) as newCompanyCode FROM companyTable where companyCode = 'AA';

TranslateCompanyCode is the stored proc that does the translation. I'd like to do this in my new code when needed. I think I might need to use a Model-Defined Function. Anyone know how I can do this?

A: 

For your scenario, I would use a JOIN. Model-defined functions are cool when you need to perform a quick function on a value (particularly without an additional query). From a performance standpoint, a JOIN will be faster and more efficient than trying to put the sub-query in a model-defined function - particularly if you are selecting more than 1 row at a time.

However, if you do still want to use Model defined functions, then this example should point you in the right direction as to how to run a query within the function. This implementation will also be more complex than just using a join but is an alternative.

Steve Michelotti
I'm using the model-defined function for this and it works great. The implementation isn't bad...it's actually better for our needs. Thanks for your help!
Dan H
That's cool. Does your implementation resemble the example from the link I sent or did you have to make any significant deviations? (just curiosity)
Steve Michelotti
No big changes, the only difference is I didn't have to put any implementation in the method I created. They have the GetProductRevenue method with implementation, and mine throws a NotImplementedException.
Dan H