简体   繁体   English

使用nHibernate的CreateSQLQuery时如何保留列顺序?

[英]How to preserve the columns order when using nHibernate's CreateSQLQuery?

Context 上下文
I'm creating a web application and one of its features is to allow DB queries via UI. 我正在创建一个Web应用程序,它的一个功能是允许通过UI进行数据库查询。 For persistance it uses nHibernate ORM. 为了持久,它使用nHibernate ORM。 Below is the repository method which handles the incoming SQL queries: 下面是处理传入SQL查询的存储库方法:

public IList ExecuteSqlQuery(string sqlQuery)
{
    var query = Session.CreateSQLQuery(sqlQuery);
    var queryResult = query.SetResultTransformer(Transformers.AliasToEntityMap).List();

    return queryResult;
}  

Problem 问题
The method above will return a List of Dictionaries each containing DictionaryEntry objects that have Key = ColumnName and Value = ColumnValue . 上面的方法将返回一个字典列表,每个字典包含具有Key = ColumnNameValue = ColumnValue的 DictionaryEntry对象。

This is all well except the column names are not in the same order as specified in the original SQL query. 除了列名与原始SQL查询中指定的顺序不同之外,这一切都很好。 For example, the following SQL query: 例如,以下SQL查询:

select FirstName, LastName, c.Name   
from SoftwareDeveloper sf  
join Certification c on  sf.SoftwareDeveloperId = c.SoftwareDeveloperId  

, returns for each row something like: ,每行的返回类似于:

["FirstName"] = "John"
["Name"] = "70-515  MCTS, .NET Framework 4, Web Applications"
["LastName"] = "Doe"  

Notice that the order of the columns is not preserved. 请注意,不保留列的顺序。 It should be: 它应该是:

["FirstName"] = "John"  
["LastName"] = "Doe"  
["Name"] = "70-515  MCTS, .NET Framework 4, Web Applications"  

UPDATE In the example above the keys of the dictionary are ordered by name, but this applies only to this example, which was intentionally kept simple. 更新在上面的示例中,字典的键按名称排序,但这仅适用于此示例,故意保持简单。 For larger queries the keys(=columns) are not ordered. 对于较大的查询,不对键(=列)进行排序。

Question
in this context(nHibernate/CreateSQLQuery), how can I preserve the column order specified in the original query? 在此上下文(nHibernate / CreateSQLQuery)中,如何保留原始查询中指定的列顺序?

UPDATE#2 更新#2
Solution
Solved by creating the following custom implementation of IResultTransformer: 通过创建IResultTransformer的以下自定义实现来解决:

public class DictionaryResultTransformer : IResultTransformer
{
    #region IResultTransformer Members
    public IList TransformList(IList collection)
    {
        return collection;
    }

    public object TransformTuple(object[] tuple, string[] aliases)
    {
        var result = new Dictionary<string, object>();
        for (int i = 0; i < aliases.Length; i++)
        {
            result[aliases[i]] = tuple[i];
        }
        return result;
    }
    #endregion
}  

In the code above the aliases are the columns names. 在上面的代码中,别名是列名称。

Sources: 资料来源:
Is it possible for nhibernate to return a query as an IDictionary instead of an entity class? 是否有可能nhibernate将查询作为IDictionary而不是实体类返回?
https://github.com/nhibernate/nhibernate-core/blob/master/src/NHibernate/Transform/AliasToEntityMapResultTransformer.cs https://github.com/nhibernate/nhibernate-core/blob/master/src/NHibernate/Transform/AliasToEntityMapResultTransformer.cs

You can't have the result as Dictionaries, as these do not define any key ordering. 您不能将结果作为词典,因为这些不定义任何键排序。 So you will have to replace the use of AliasToEntityMap with something that builds a data structure that preserves ordering (implement IResultTransformer). 因此,您必须将AliasToEntityMap的使用替换为构建保留排序的数据结构(实现IResultTransformer)。 SortedDictionary might work if you give it a comparer that internally maps from column name to column index. 如果为其提供一个内部映射从列名到列索引的比较器,SortedDictionary可能会起作用。

Note that if you call List() directly without using a result transformer, you should get a list of object arrays, in which the order of elements follows that which is specified in the select clause. 请注意,如果直接调用List()而不使用结果转换器,则应获取对象数组列表,其中元素的顺序遵循select子句中指定的顺序。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 使用CreateSQLQuery Nhibernate - Using CreateSQLQuery Nhibernate 在NHibernate中将CreateSQLQuery与INSERT查询一起使用 - Using CreateSQLQuery with INSERT query in NHibernate NHibernate:如何使用带有本机 SQL 的 CreateSQLQuery 返回标量值? - NHibernate: How to return scalar value using CreateSQLQuery with native SQL? NHibernate CreateSQLQuery - NHibernate CreateSQLQuery 如何在 DataGridView 中显示 nHibernate CreateSQLQuery 的结果? - How to show the result of nHibernate CreateSQLQuery in DataGridView? 使用SQLite时,Fluent Nhibernate是否在所有CreateSQLQuery调用上自动设置外键约束? - Does Fluent Nhibernate automatically set Foreign Key Contraints on all CreateSQLQuery calls when using SQLite? NHibernate:如何使用CreateSQLQuery返回标量值列表(从一列)? - NHibernate: How to return list of scalar values (from one column) using CreateSQLQuery? NHibernate CreateSQLQuery中的可能问题 - Possible issues in NHibernate CreateSQLQuery Nhibernate:使用已定义的映射使用CreateSqlQuery调用存储过程 - Nhibernate: invoke stored procedure with CreateSqlQuery using already defined mapping NHibernate:将createSQLQuery与一对一映射一起使用会导致额外的查询 - NHibernate: Using createSQLQuery with one-to-one mapping causing extra queries
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM