views:

39

answers:

2

Hi all,

I'm working with a pretty nasty looking, but non-changable db.

We've got a table, called "Locations" with column, "Position"

This column has values like "A SHELF 5"

Which means "Case A, Shelf 5". In an ideal world, I'd have a Case and Shelf column, with the values "A" and 5, respectively.

What I'm wondering, is if there's a way to define some "Virtual" (maybe not the right word) columns, in my entity classes, so that once I do that, I can just query the table as if those columns existed (Selects, Where clauses, Group By, etc)?

Admittedly, I'm pretty new to Linq, so I'm not even using the right technical terms to describe what I want to do. Which makes it harder to find the answers. So if someone just wants to point me to any introductory documentation that would even help me get started, I'd consider that a valid answer.

To make it more complicated, the column is only structured as above, depending on the value of some other columns, but I'll not go there.

thanks for taking the time to read this. Past experience tells me the SO crew is the best!

A: 

I'm used to Ling-to-Sql rather than Linq-to-Entities, but this should work

partial class Location
{
     public string Case 
     {
         get
         {
             int pos = Position.IndexOf(' ');
             if (pos == -1)
                 throw new Exception();
             return Position.Substring(pos);
         }
     }

}
James Curran
that works great for selecting: However, when I try to use it in a constructed Where clause (admittedly, using method for generating dynamic where clauses, describe here: http://stackoverflow.com/questions/3782305/linq-dynamic-query-contruction-query-moves-to-client-side )
donundeen
error : The member 'Case' has no supported translation to SQL.
donundeen
[sorry about the multiple posts, I keep hitting "return"]. Do I need to create set method as well, or something else?
donundeen
A: 

Anything you do to split the column into multiple other columns within Entity Framework isn't going to translate into SQL within your queries. That means that anything filtering on the derived columns is going to do its filtering on the client after retrieving all the rows not filtered by other where-clause elements. This may not be a big deal, but it's something you need to bear in mind.

Doing a calculated column itself is a simple matter of creating a new property in a partial code file (as demonstrated by James Curran's answer). Your objects should use that property just fine, even in queries.

Personally, though, in your situation, I'd look at creating views at the database level. A view of the Locations table could be setup to break the offending column up into two other columns without too much difficulty and the additional overhead can payoff in the long term. You'll have to watch your updates, but for most purposes this would serve your purposes.

Jacob Proffitt
thanks, I don't have that kind of DB access, sadly. And I've actually implemented this in the past with raw SQL, so I'm a little surprised I can't describe that mapping here.
donundeen
does this problem relate in any way to Model Defined functions, as discussed here: http://blogs.msdn.com/b/efdesign/archive/2009/01/07/model-defined-functions.aspx ? Because all I want is that when translating to SQL, the column name be "SUBSTRING(Position, 1,1) AS SHELF" instead of "SHELF." And I want to be able to define that on the Column property (like: [Column(Function="SUBSTRING(CurUnitPosition, 1,1)")] ). It doesn't seem complicated, which is why I don't understand why I can't do it. Is this available in C# 4.0? Thanks!
donundeen