如何使用 NHibernate 获得不同的首字母列表 [英] How to get a distinct list of first letters with NHibernate

查看:69
本文介绍了如何使用 NHibernate 获得不同的首字母列表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我进入了一个使用 MVC2 & 的项目的中间.休眠.我以前没有使用过 NHibernate,所以这个问题看起来很简单,但我有点超出我的深度.

I have been landed into the middle of a project that is using MVC2 & NHibernate. I haven't worked with NHibernate before, so as simple as this question might seem, I'm a bit out of my depth.

我必须为多个公司组创建一个目录.第一个任务是获取每个公司的第一个字母的不同列表.

I have to create a directory for a number of Company Groups. The first task is to get a distinct list of the first letter for each company.

所以如果我们有

A公司1
ACompnay2
B公司1
D公司1
DCompany2
E公司1

ACompany1
ACompnay2
BCompany1
DCompany1
DCompany2
ECompany1

我需要一个像

A B D E(注意,没有C")

A B D E (note, there's no 'C')

有人可以向我提供我需要做什么的大纲吗?谢谢

Can somebody please provide me with an outline of what I need to do? Thanks

推荐答案

我想知道您是否从错误的方向接近这个问题.NHibernate 用于将您的数据库对象映射到域对象.您可以创建一些使用公式来获取不同字符的自定义地图文件,但我认为这是简单 SQL 查询就足够的情况之一.

I wonder if you are approaching this from the wrong direction. NHibernate is used to map your database objects to your domain objects. You could create some custom map file that use a formula to get you the distinct characters but I think this is one of those cases where a simple SQL query would suffice.

另一种方法是将所有公司加载到内存中并使用 linq 获取名称.

The alternative would be load all the companies into Memory and use linq to get the names.

companies.Select(company => company.Name.Substring(0, 1));

有时我在使用模拟框架时会感到困惑,并且很难模拟特定实体,直到我退后一步,我才意识到模拟自己的实体会更容易、更清晰.

I sometimes get blinkered when using a mocking framework and struggle to mock a particular entity, it isn't till I step back I realise that it would be easier and cleaner to mock my own entity.

这篇关于如何使用 NHibernate 获得不同的首字母列表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆