1

I want to convert the following SQL Query to a SubSonic Query.

SELECT [dbo].[tbl_Agency].[ParentCompanyID] FROM [dbo].[tbl_Agency] WHERE REPLACE(PhoneNumber, ' ', '') LIKE REPLACE('%9481 1111%', ' ', '')

I thought I would do it like below, but I just can't get it to produce valid SQL.

     //SubSonic
     string agencyPhoneNumber = "9481 1111";
     SubSonic.SqlQuery subQueryagencyPhoneNumber = new SubSonic.Select(Agency.ParentCompanyIDColumn.ColumnName);
            subQueryagencyPhoneNumber.From(Agency.Schema.TableName);

     //WHERE
     subQueryagencyPhoneNumber.Where("REPLACE(" + Agency.PhoneNumberColumn.ColumnName + ", ' ', '')").Like("%" + agencyPhoneNumber + "%");

Does anyone out there know how to fix this - I'm using SubSonic 2.2. I feel like I'm taking crazy pills here - this should be straightforward, right?

Cheers, JohnBob

4

2 回答 2

1

如果最糟糕的查询变得最糟糕,您仍然可以使用 .CodingHorror() 方法使用亚音速,如@ http://subsonicproject.com/docs/CodingHorror所述

只要您选择 subsonic 在您投射集合时知道的有效列名 - > .ExecuteAsCollection() 您就可以了,并且仍然可以使用您的活动记录。

当我有非常大的复杂查询时,这为我节省了很多时间。只要确保您仍然使用参数并清理您的语句,以便清理您的输入。

于 2010-03-19T08:58:05.650 回答
1

我认为您需要将like 表达式添加到内联SQL。
就像是:

var whereSql = string.Format("REPLACE({0}, ' ', '') Like '%{1}%'", Agency.PhoneNumberColumn.ColumnName, agencyPhoneNumber);

subQueryagencyPhoneNumber.Where(whereSql);

请参阅Calling an SQL function from a Subsonic.Select中的类似问题

于 2010-03-15T12:17:00.827 回答