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 Query
- LINQ - How to use distinct with order by?
- How do I use a custom comparer with the Linq Distinct method?
- How to use distinct with group by in Linq to SQL
- How can I use order by and group by in Linq Extraction Method and With multiple data?
- Linq using how to use order by and group by with left join?
- How to use order by clause in linq query with dynamic conditions?
- How to use LINQ to select object with minimum or maximum property value
- How to use LINQ Distinct() with multiple fields
- How to use LINQ with dynamic collections
- How do you use LINQ with Sqlite
- How to use index/position with Where in LINQ query language?
- How to get distinct with highest value using Linq
- How to use Dapper with Linq
- How to use LINQ to order within groups
- How do I use a WPF TreeView HierarchicalDataTemplate with LINQ to Entities?
- How to build a hierarchy with use Linq to object?
- How to: Use async methods with LINQ custom extension method
- How can I use Linq with a MySql database on Mono?
- How do I use Linq ToDictionary to return a dictionary with multiple values in the dictionary items?
- LINQ How to define a default type for use with ElementAtOrDefault Operator
- How to use a Func in an expression with Linq to Entity Framework?
- How to use linq `Except` with multiple properties with different class?
- How to use C# LINQ Union to get the Union of Custom list1 with list2
- How do I properly use LINQ with MySQL?
- How can I use linq to return integers in one array that do not match up with an integer property of another array?
- How can I switch that code to use LINQ
- how to use LINQ to SQL with mySQL
- How to use a Distinct on a column using Linq
- how to use Linq count with group by in C#
- How to use a LINQ query to include index in Select with F#
More Query from same tag
- Check duplicates before update
- why doesn't Dictionary<TKey, TValue>.KeyCollection Class have its own Contains method?
- Single statement query
- how to set string variable as property in subquery
- Subobject collection as columns in a variable columns table
- MVC4 lambda expression for SimpleMembership
- How does one populate multiple lists with a LINQ statement
- Is there a way to conditionally include or exclude a navigation property when querying Database using EF Core
- Linq query return 1 item instead of all
- EF Linq include nested arrays
- Using an Expression in a compiler-generated expression tree
- Class-Scoped Linq in-memory Query
- How to retrieve last 5 records using LINQ method or query expression in C#
- How to delete an item from a Dictonary using LINQ in C#
- Linq PredicateBuilder with conditional AND, OR and NOT filters
- How do I get an object from a relational database with LINQ?
- Identifying Unique Values in a C# List
- Linq to Object/XML where element doesn't exist
- How to filter a flattened LINQ result in C#
- The best way to build Dynamic LINQ query
- How to select minimum element without transforming it?
- C# Not able to use CopyToDataTable() function when grouping fields by LINQ & Datatable
- Use type derived from GetType() without using Switch
- How to Query Icollections of Entity Framework Code First Data
- How do i combine 2 Linq queries into 1?
- 'Timeout Expired' error when buffer cache clean
- Determine if any property value is not null and matches value in another list
- How can I create a dynamic multi-property Select on an IEnumerable<T> at runtime?
- Slickest way to put objects into separate lists based on a property value
- Linq to Entities versus Linq to Objects Performance Issue