开发者

Query Concatenated Field (using SubSonic)

Is there a way to query against a concatenated field using MS SQL? For instance, what I want to do is something like:

Select FirstName+' '+LastName as FullName from Attendees where FullName like '%Joe Schmoe%'

The above doesn't work. What I have fou开发者_运维知识库nd works is:

Select * from Attendee where FirstName+' '+LastName like '%Joe Schmoe%'

but I can't figure out how to do that using a SubSonic SqlQuery. I have a number of joins and OR statements added dynamically that I don't want to have to write out the sql manually.

Any help/ideas?


how about this:

SELECT FullName 
FROM (SELECT FirstName+' '+LastName as FullName 
      FROM Attendees) X
WHERE FullName LIKE '%Joe Schmoe%'


You can just specify your condition in the Where part of your query as follows:

List<Attendee> cardHolders = new YourDB()
  .Select
  .From<Attendee>()
  .Where(AttendeeTable.FirstNameColumn + " + ' ' + " + AttendeeTable.SurnameColumn + " AS FullName")
  .Like("%Joe Schmoe%")
  .ExecuteTypedList<Attendee>();


It seems it isn't possible in SubSonic


So late to the game but, if your using the SubSonic Select() you can use the SQL CONCAT to concatenate multiple columns before loading them into your DataTable for your DataGrid so for instance

    SubSonic.SqlQuery query = new SubSonic.Select(
     "CONCAT(FirstName, ' ', LastName) as FullName"
     ).From("Attendees").Where("LastName").Like("%Schmoe%");
    return query;

This was something I was looking for as well and just tried to see if the SubSonic would let me use the SQL CONCAT within the Select portion of the SubSonic.SqlQuery. Hope this helps anyone else down the road.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜