Uploaded image for project: 'NHibernate [Moved to GitHub]'
  1. NH-2912

LINQ Projection of Int32 using ToString method causes "cast as char" instead of "cast as nvarchar" in MsSql dialect

    Details

    • Sprint:

      Description

      Casting as char has side effect of property being padded with spaces. Example:

      Assuming MyClass has Int32 "ID" property and string "Title" property this type of command:

      CustomerTrade.Linq.Select(x => new SelectListItem()

      { Text = x.Title, Value = x.ID.ToString() }

      ).ToList();

      In NHibernate 3.1 generates this SQL:

      select customertr0_.Title as col_0_0_,
      customertr0_.ID as col_1_0_
      from CustomerTrade customertr0_
      where customertr0_.Active = 0 /* @p0 */

      in NHibernate 3.2GA "cast" appeared:

      select customertr0_.Title as col_0_0_,
      cast(customertr0_.ID as char) as col_1_0_
      from CustomerTrade customertr0_
      where customertr0_.Active = 0 /* @p0 */

      This query returns "ID" with a lot of spaces at the end. Since we are using it with ASP.NET MVC to populate SelectListItem (which declares "Value" as string type) this is a problem for us.

      Suggested solution:

      I downloaded 3.2GA sources and added one line of code:
      Modified file: src\NHibernate\Dialect\MsSql2000Dialect.cs
      Modified method: RegisterFunctions()
      Modification: line added: RegisterFunction("str", new SQLFunctionTemplate(NHibernateUtil.String, "cast(?1 as nvarchar)"));

      After recompiling using Nant 0.9, NHibernate generates correct SQL:

      select customertr0_.Title as col_0_0_,
      cast(customertr0_.ID as nvarchar) as col_1_0_
      from CustomerTrade customertr0_
      where customertr0_.Active = 0 /* @p0 */

      please note "cast as nvarchar" instead of "cast as char". I also attach patch (if that helps).

        Attachments

          Issue links

            Activity

              People

              • Assignee:
                hazzik Alexander Zaytsev
                Reporter:
                sebbie Sebastian Pienio (Inactive)
              • Votes:
                0 Vote for this issue
                Watchers:
                2 Start watching this issue

                Dates

                • Created:
                  Updated:
                  Resolved:

                  Who's Looking?