score:0
This might be what your looking for:
Page firstPage = Session.Linq<Page>()
.OrderBy(page => page.Index)
.FirstOrDefault(page=> page.Location.URL == "some-location-url");
I'm making the assumption that the page has a Location property that relates back to the Location it belongs to and the .Index would be the property you want to order with.
score:0
Run the query against Page
instead of against Location
: you don't need to return the Location
record at all.
(from p in AllPages
where p.Location.URL == "some-location-url"
select p).FirstOrDefault();
[Almost always when I get stuck with writing a LINQ query, I find it helps to start building the query from the bottom-most object in the parent-child relationships involved.]
score:0
Could you not add a order by directly to the mapping of the collection. You could then do
Page firstPage = Session.Linq<Location>()
.Where(location => location.URL == "some-location-url")
.Select(location => location.Pages.FirstOrDefault()).FirstOrDefault();
The FirstOrDefault may stop NHibernate making all the selects.
No Guarantee on this as I haven't got linq to nhibernate set up to test it.
Source: stackoverflow.com
Related Query
- How to return first object of a collection from its parent
- How to perform .Max() on a property of all objects in a collection and return the object with maximum value
- How to get first object out from List<Object> using Linq
- How to understand the following C# linq code of implementing the algorithm to return all combinations of k elements from n
- How to return LINQ object from method?
- linq how to select the parent from a collection where the parent contains child items in another collection
- How to get list for a property of a class from its collection
- How do I get the first value from this collection using Linq to Entities?
- How do I return a LinkedList collection from a lambda?
- How to get the second repeated item from a collection of objects using LINQ to object
- How do I get SingleOrDefault to return object by reference from a list?
- How do I return a new object from a mocked repository using a LINQ Expression?
- How do i return a certain max number of items from a collection using linq
- How can I return a object with the sum of all objects from a list using Linq?
- How to Filter Child Collection from each Parent Entity in the Parent Collection using Linq
- How do I group items from a collection using LINQ and return the shaped data according the collection type
- How do I use LINQ to return the first item from each set of Grouped data (In VB.Net)
- Linq query to join two tables and return object from first table - PagedList used
- How to project a Mongo LINQ query to only return child object from document?
- How to return two values from single methods and set two parent class property calling function 1 time?
- How to remove each item that appears in first collection from second collection using linq?
- How to Get a Object from IEnumerable collection using LINQ Lambda?
- How to return only collection of certain entries from a complex type after query?
- How can I include a field from a parent object in my LINQ?
- How do I get the object with the second latest date from a collection of objects
- How to execute a linq query for each item in a list , use it in the where clause and return a collection from the result of each query?
- How can i return an object with a dictionary property from dapper repo call
- How can I return an IOrderedEnumerable from a Collection based on OrderBy "property"
- How to place every child node from a specific parent node into its own parent node
- How to return an object (or a list of objects) of anonymous type from a LINQ query to the UI (Console App) in C#?
More Query from same tag
- Convert stored procedure to Linq statement
- Checking duplication using LINQ in collection
- Using LINQ intersect function with a non-primitive type
- C# Linq convert collection using groupby
- explain these Linq Codes
- Linq: Get rows where values exists in another table
- linq to xml - get rid of blank xmlns
- How to link data from nested relational tables in a C# query
- how to Split a list/array based on index of array
- How would I write this as a single LINQ query?
- Is StringComparer.CurrentCulture the right choice to use in this case?
- How do you compile LINQ queries against CRM 2011?
- How properly work with LINQ to XML?
- Why my linq query projecting one item in the model class?
- Linq Group Join Items into List
- Returning list of properties from a list of objects
- Pass int array in where clause of LINQ Query
- LINQ to Entities does not recognize the method (on a related entity)
- Extension method to Order children alphanumerically in a tree-like generic list
- Filtering IQueryable sub list
- Listing words from array with must letters using linq
- "Turning" an IEnumerable<IEnumerable<T>> 90 degrees
- How do I specify the sorting field and direction in a linq query at runtime?
- How to better write this EF core 6 Group by Query
- Where is the "Fold" LINQ Extension Method?
- How do you give the resulting set a name when calling Linqpad's .Dump() method on a List<>?
- C# Read/Write XML File
- Checklist Box Selected Items from LinQ
- Composing F# code quotations programmatically
- Using LINQ to generate Highchart column table data