2

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.Localization.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!

1
  • public class MyComparer : IEqualityComparer<ILocalization> { public bool Equals(ILocalization x, ILocalization y) { return (x.Key == y.Key); } public int GetHashCode(ILocalization obj) { return obj.Key.GetHashCode(); } } Commented Oct 6, 2011 at 10:06

1 Answer 1

2

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()); 
Sign up to request clarification or add additional context in comments.

2 Comments

I need IQeryable for CustomBinding on Telerik Grid, not List
If you must have IQueryable, then you can add .AsQueryable() after the Distinct call.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.