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!
- 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(); } }Andrii Spivak– Andrii Spivak2011-10-06 10:06:07 +00:00Commented Oct 6, 2011 at 10:06
Add a comment |
1 Answer
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()); 2 Comments
Andrii Spivak
I need IQeryable for CustomBinding on Telerik Grid, not List
jasonp
If you must have IQueryable, then you can add .AsQueryable() after the Distinct call.