How to return the first collection object from your parent

I want to do the following with a single database query, if possible.

public class Location
{
    public string URL {get;set;}
    public IList<Page> Pages {get;set;}
}

Page firstPage = Session.Linq<Location>()
                .Where(location => location.URL == "some-location-url")
                .Select(location => location.Pages).FirstOrDefault();

My goal is based on the current location url, returning the first page object from its collection.

I tried several different ways, and they all all seem to do a lot of queries to get the desired page object.

Any help is appreciated!

Dave Ninja

+3
source share
3 answers

This may be what you are looking for:

Page firstPage = Session.Linq<Page>()
.OrderBy(page => page.Index)
.FirstOrDefault(page=> page.Location.URL == "some-location-url");

I assume that the page has a Location property that belongs to the place where it belongs, and .Index is the property you want to order.

0

Page Location: Location.

(from p in AllPages
where p.Location.URL == "some-location-url"
select p).FirstOrDefault();

[ , LINQ, , - .]

0

.

Page firstPage = Session.Linq<Location>()
              .Where(location => location.URL == "some-location-url")
              .Select(location => location.Pages.FirstOrDefault()).FirstOrDefault();

FirstOrDefault NHibernate, .

, linq nhibernate, .

0

Source: https://habr.com/ru/post/1703098/


All Articles