开发者

Get Distinct IQueryable with NHibernate

开发者 https://www.devze.com 2023-04-10 21:20 出处:网络
I have table in db with fields: Id, Key, Value. I have several identical Key\'s I want to get IQuerable collection of table via NHibernate 3.2.0.4 with distinct by Key.

I have table in db with fields: Id, Key, Value. I have several identical Key's

I want to get IQuerable collection of table via NHibernate 3.2.0.4 with distinct by Key.

I tried do it so:

1.var items = dr.Localization.GetQuery().Distinct(new MyComparer());

and received:

Could not parse expression 'value(NHibernate.Linq.NhQueryable`1[AbstractDataRepository.Domain.ILocalization]).Distinct(value(LEditorExtension.Presentation.Controllers.MyComparer))': This overload of the method 'System.Linq.Queryable.Distinct' is currently not supported.

2.var items = dr.Localizati开发者_高级运维on.GetQuery().GroupBy(x => x.Key).Select(g => g.First());

received null in 'items'.

dr.Localization.GetQuery().GroupBy(x => x.Key): Expression cannot contain lambda expressions

Is there any way to solve this problem? Thanks!


The problem here is that your query is being fired against the db where your custom comparer is invalid as it cant be translated to a valid sql. Thus if you think you can do the distinct in sql then you can try by specifying which property(s) you want it to be distinct.

If you still wanna use your custom comparer do a List() first and then do a distinct on it.

dr.Localization.GetQuery().List().Distinct(new MyComparer());
0

精彩评论

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

关注公众号