This is a followup to this question about custom paging in ASP.NET.
Say I have a table defined:
CREATE TABLE Person
(
ID int identity(1,1) not null primary key,
FirstName varchar(50),
LastName varchar(50)
)
And I have it mapped to a class via the Linq to SQL designer. I've defined this property on the Person
class:
public string Name
{
get
{
if (FirstName != null && FirstName != "" && LastName != null && LastName != "")
return String.Format("{0} {1}", FirstName, LastName);
else if (FirstName == null || FirstName == "")
return LastName;
else if (LastName == null || LastName == "")
return FirstName;
else
return "";
}
}
Say I've got a grid that is sorted by a column bound to that Name property, and I want to do paging. The natural way to do that with Linq to SQL is something like:
MyDBContext db = new MyDBContext();
var myData = db.Persons.OrderBy(p => p.Name).Skip(pageSize * pageIndex).Take(pageSize);
myGrid.DataSource = myData;
myGrid.DataBind();
Linq to SQL throws an error here, though, because it can't convert the Name property to SQL. I can put .AsEnumerable()
in the string of filters, as noted in an answer to the question I just referenced, but that means I'm pulling all rows to the web server and sorting them there, which is not ideal if there are enough records in the table. Filtering and sorting should happen in the database.
The simple answer is to convert that Name
property to SQL and make it part of a SPROC that returns the data, or a view, or something. But that doesn't feel right to me because I'm putting display logic in my database layer. It's even worse if the situation is more complex than my somewhat contrived example, and the property defined is more nontrivial.
Is there a way out, or am I stuck having to choose between performance and separation of concerns? Or is my concern about that kind of display logic in the database unfounded?