Thank you Guys, Gunnar, I will follow your tip, and if I have a Select in my query? Order could be before Select or after?
query = query.Select(x => new ViewModel(x.Name, x.Code)).OrderBy(x => x.Code).Take(10).ToList(); or query = query.OrderBy(x => x.Code).Select(x => new ViewModel(x.Name, x.Code)).Take(10).ToList(); or does not matter? Thank you. On Mon, Dec 23, 2013 at 7:13 PM, Gunnar Liljas <[email protected]>wrote: > You should do: > > var query = session.Query<Entity>(); > > if (condition) > { > query = query.Where(x => x.Name.StartWith("A")); > } > > > var result = query.OrderBy(x=>x.Name).Take(10).ToList(); > > Everything before the ToList will be handled by the detabase. > > While adding the Take before anything else perhaps will work (due to > strange stuff), it's just wrong since it means: > > Take the first 10 entities from the table (in table/index order, i.e > irrelevant), and filter those by name. You may get zero results, even if > you have thousands of matching entities. > > Always order your results by something, to get predictable results. > > /G > > > > 2013/12/23 Allan Fagner <[email protected]> > >> Hi, >> >> I'm not sure if I'm following your question correctly. But here is the >> drill: the method ToList() is the trigger to sent your query to the >> database. Given the code you posted, that method will trigger a query more >> or less like that depending on the database: >> >> Select Top 10 From Entity Where Name Like "A%" >> >> Is that the best approach? I don't know. :) Only you can answer that.g >> >> -- >> Allan Ferreira >> .NET Software Architect >> .NET Senior Consultant >> M: +55-41-98098597 >> E: [email protected] >> >> >> On Mon, Dec 23, 2013 at 3:35 PM, Felipe Oriani <[email protected]>wrote: >> >>> Hi friends. >>> I have a query like this: >>> >>> >>> ----------------- >>> >>> var query = session.Query<Entity>().Take(100); >>> >>> if (condition) >>> { >>> query = query.Where(x => x.Name.StartWith("A")); >>> } >>> >>> var result = query.ToList(); >>> >>> ----------------- >>> >>> I would like to know, if in the queryable Take method execute my query >>> before ToList() method? >>> >>> I want to know because I have more than 100 records in the table and it >>> is spread on database and it just take 10 records starting with "A". If >>> Yes, what is the best approuch to do it? >>> >>> >>> Thank you. >>> >>> >>> -- >>> ______________________________________ >>> Felipe B Oriani >>> felipeoriani.com.br [email protected] >>> >>> -- >>> You received this message because you are subscribed to the Google >>> Groups "nhusers" group. >>> To unsubscribe from this group and stop receiving emails from it, send >>> an email to [email protected]. >>> To post to this group, send email to [email protected]. >>> Visit this group at http://groups.google.com/group/nhusers. >>> For more options, visit https://groups.google.com/groups/opt_out. >>> >> >> -- >> You received this message because you are subscribed to the Google Groups >> "nhusers" group. >> To unsubscribe from this group and stop receiving emails from it, send an >> email to [email protected]. >> To post to this group, send email to [email protected]. >> Visit this group at http://groups.google.com/group/nhusers. >> For more options, visit https://groups.google.com/groups/opt_out. >> > > -- > You received this message because you are subscribed to the Google Groups > "nhusers" group. > To unsubscribe from this group and stop receiving emails from it, send an > email to [email protected]. > To post to this group, send email to [email protected]. > Visit this group at http://groups.google.com/group/nhusers. > For more options, visit https://groups.google.com/groups/opt_out. > -- ______________________________________ Felipe B Oriani felipeoriani.com.br [email protected] -- You received this message because you are subscribed to the Google Groups "nhusers" group. To unsubscribe from this group and stop receiving emails from it, send an email to [email protected]. To post to this group, send email to [email protected]. Visit this group at http://groups.google.com/group/nhusers. For more options, visit https://groups.google.com/groups/opt_out.
