score:0
Not sure about the performance impact compared to the answer in OP's comments, but this could be subqueried for readability!
query =
(from distinctAcao in
(from acao in query
join itemAuditoria in Session.Query<ItemAuditoria>() on acao.Id equals itemAuditoria.Acao.Id
join auditoria in Session.Query<Auditoria>() on itemAuditoria.Auditoria.Id equals auditoria.Id
join maquina in Session.Query<Maquina>() on auditoria.Maquina.Id equals maquina.Id
select acao).Distinct()
orderby maquina.Nome, distinctAcao.Numero
select distinctAcao);
edit:
So sorry for overlooking that it didn't compile - it did in my head ;) Corrections on the way!
Because we're doing subquery here, the result of which is a list of acao
items which don't have a Nome
field, we cannot sort by it after we leave the subquery. Therefore we can either:
Sort inside subquery, then eliminate duplicates.
query=( from distinctAcao in ( from acao in query join itemAuditoria in Session.Query<ItemAuditoria>() on acao.Id equals itemAuditoria.Acao.Id join auditoria in Session.Query<Auditoria>() on itemAuditoria.Auditoria.Id equals auditoria.Id join maquina in Session.Query<Maquina>() on auditoria.Maquina.Id equals maquina.Id orderby maquina.Nome,acao.Numero select acao ).Distinct() select distinctAcao );
Result: sorted, unique
acao
s listCons: first sorting, then filtering might impact performance
Return a better, combined object from the subquery, which will expose new fields from joined tables.
query2=( from distinctMix in ( from acao in query join itemAuditoria in Session.Query<ItemAuditoria>() on acao.Id equals itemAuditoria.Acao.Id join auditoria in Session.Query<Auditoria>() on itemAuditoria.Auditoria.Id equals auditoria.Id join maquina in Session.Query<Maquina>() on auditoria.Maquina.Id equals maquina.Id select new { Id=acao.Id,Numero=acao.Numero,NomeFromMaquina=maquina.Nome } ).Distinct() orderby distinctMix.NomeFromMaquina,distinctMix.Numero select distinctMix );
Result: quickly sorted, unique (
acao
+more) listCons: returned object will be of different type than original
query
that was used to iterateacao
s, so assigningquery=from acao in query select new { /* something else than acao */ };
won't work (hencequery2
in this code example)Use answer from OP's comments :)
P.S.
This time the code compiles for sure - test code available at https://ideone.com/mDRRle :>
Source: stackoverflow.com
Related Articles
- Linq to SQL order by with Distinct
- LINQ - How to use distinct with order by?
- Linq Code with Distinct or GroupBy not working
- Order by, distinct and select top 5 results with linq
- Preserving order with LINQ
- Multiple Order By with LINQ
- Distinct by property of class with LINQ
- LINQ Select Distinct with Anonymous Types
- Distinct not working with LINQ to Objects
- LINQ query with Distinct and Union
- Preserve order of values with linq and ToLookup()
- LINQ Order By Descending with Null Values on Bottom
- How to get distinct with highest value using Linq
- Using Distinct with LINQ and Objects
- Order by does not work with Concat() in LINQ
- Can you reverse order a string in one line with LINQ or a LAMBDA expression
- Entity Framework - Linq query with order by and group by
- How do I use a custom comparer with the Linq Distinct method?
- Linq Query with SUM and ORDER BY
- Select Distinct List of Words from Array with LINQ
- Dynamic linq order by on nested property with null properties
- Distinct not working with LINQ
- LINQ - writing a query with distinct and orderby
- How to use distinct with group by in Linq to SQL
- Can we control LINQ expression order with Skip(), Take() and OrderBy()
- Distinct in LINQ with anonymous types (in VB.NET)
- LINQ Queries with dynamic Order By
- LinQ distinct with custom comparer leaves duplicates
- LINQ join with distinct resultset
- Getting an invalid cast exception when trying to order a list of objects with linq
- Adding a large amount of records using LINQ
- Tricky string transformation (hopefully) in LINQ
- Renaming Identity Tables and Maintaing Relationship
- linq query close my wpf application
- How can I Write This SQL Query to Linq Query
- How to filter SelectList with linq
- How to find all objects with an attribute that matches any of the values in another collection?
- Elegant way to add a specific character after each char in a string
- List Orderby property from nullable object
- LINQ to Entities does not recognize the method 'System.String ToString(System.Object)' method error occurs with nullable fields
- Find the newest item in a C# generic list using Linq
- LINQ to SQL Pivot table in VB.net
- Linq query count
- Linq insert on primary key field that is autosync
- Cannot convert Linq.IOrderedEnumerable<T> to Linq.IQueryable<T>
- Why the default that i see in SQL Server for the fields does no reflected in LINQ
- Entity Framework query joins and group by issue
- C# .First() vs [0]
- Speed up return of linq/entity result
- Groupby multiple date properties by month and year in LINQ