This thread looks to be a little on the old side and therefore may no longer be relevant. Please see if there is a newer thread on the subject and ensure you're using the most recent build of any software if your question regards a particular product.
This thread has been locked and is no longer accepting new posts, if you have a question regarding this topic please email us at support@mindscape.co.nz
|
Hello, I show my scenario. I've a table called LISTINI with an auto-increment key field ID. I must do a SELECT DISTINCT to a field called LISTICOLISTI and assign result to a lookupedit.
This is my code: Query _query = new Query(); How can I specify field LISTICOLISTI in Distinct? Thanx |
|
|
SELECT DISTINCT selects the distinct values of the specified columns. When you select an entity using Find<TEntity>, all columns are included in the result and hence in the distinctness test, so you will get all entities. So to get only the distinct values of LISTICOLISTI, you must use a projection. This is easiest to do with LINQ: uow.Query<Listini>().Select(li => li.Listicolisti).Distinct(); Note this gets you only the Listicolisti values (as a sequence of strings or integers or whatever), not the entities associated with those values. This would make no sense because there could be multiple entities associated with the same value. So if you did need the full entities, but keyed by distinct Listicolisti values, you would have to use a GroupBy operation instead of a Distinct. |
|
|
Ok, I think I need the GroupBy method. I tried to use: _query.Group = Group.By(Entity.Attribute("Listicolisti")); but don't work. uow.Query<> not exist. I must use: uow.Find<Listini>(_query).Select(li => li.Listicolisti).GroupBy() ? |
|
|
If you use Find<Listini> then the grouping will be done on the client rather than in the database. For example: uow.Find<Listini>(_query).GroupBy(li => li.Listicolisti); will retrieve all entities from the database, then arrange them into groups by the Listicolisti value. IUnitOfWork.Query<T> is an extension method which is part of the LINQ provider. You'll typically have a wrapper property for it (MyModelUnitOfWork.Listinis) or you can bring it into scope by putting a using Mindscape.LightSpeed.Linq; declaration in your source file. If you use Query instead of Find, then the grouping is done in the database. |
|