New Posts New Posts RSS Feed: how can i get the last element of a query?
  FAQ FAQ  Forum Search   Calendar   Register Register  Login Login

how can i get the last element of a query?

 Post Reply Post Reply
Author
monkeyking View Drop Down
Groupie
Groupie
Avatar

Joined: 04-Mar-2009
Location: brisbane
Posts: 68
Post Options Post Options   Quote monkeyking Quote  Post ReplyReply Direct Link To This Post Topic: how can i get the last element of a query?
    Posted: 08-Jul-2009 at 8:27pm
once I query from the DomainModelEntityManager, I get a set of data, how can i get the first and last element of that set of data?
Back to Top
GregD View Drop Down
IdeaBlade
IdeaBlade
Avatar

Joined: 09-May-2007
Posts: 374
Post Options Post Options   Quote GregD Quote  Post ReplyReply Direct Link To This Post Posted: 09-Jul-2009 at 11:44am

      var query = _mgr.Customers.OrderBy(c => c.CompanyName);
      Customer aCustomer = query.FirstOrNullEntity();
      Console.WriteLine("First customer: {0}", aCustomer.CompanyName );
      Customer anotherCustomer = query.ToList().LastOrDefault();
      Console.WriteLine("Last customer: {0}", anotherCustomer.CompanyName);



The query must be forced to execute immediately for LastOrDefault(); hence the ToList() call for that one.


Back to Top
monkeyking View Drop Down
Groupie
Groupie
Avatar

Joined: 04-Mar-2009
Location: brisbane
Posts: 68
Post Options Post Options   Quote monkeyking Quote  Post ReplyReply Direct Link To This Post Posted: 09-Jul-2009 at 5:47pm
but some time I don't have 'ToList()' method, how can I make it appear? it only has 'ToHashSet()' and others, no 'ToList()'!
Back to Top
Suah View Drop Down
Newbie
Newbie
Avatar

Joined: 06-Jul-2009
Posts: 16
Post Options Post Options   Quote Suah Quote  Post ReplyReply Direct Link To This Post Posted: 19-Jul-2009 at 5:37pm
using System.Collections.Generic;
so it will appear , you may like to learn more csharp basic stuff
Back to Top
monkeyking View Drop Down
Groupie
Groupie
Avatar

Joined: 04-Mar-2009
Location: brisbane
Posts: 68
Post Options Post Options   Quote monkeyking Quote  Post ReplyReply Direct Link To This Post Posted: 20-Jul-2009 at 7:08pm
i think this is advanced knowledge. thanks for replying
Back to Top
monkeyking View Drop Down
Groupie
Groupie
Avatar

Joined: 04-Mar-2009
Location: brisbane
Posts: 68
Post Options Post Options   Quote monkeyking Quote  Post ReplyReply Direct Link To This Post Posted: 29-Jul-2009 at 4:47pm
no, I don't think that's soluction, even I include System.Collections.Generic, it still doesn't have 'ToList()' method.
Back to Top
GregD View Drop Down
IdeaBlade
IdeaBlade
Avatar

Joined: 09-May-2007
Posts: 374
Post Options Post Options   Quote GregD Quote  Post ReplyReply Direct Link To This Post Posted: 30-Jul-2009 at 2:49pm
Originally posted by monkeyking

no, I don't think that's soluction, even I include System.Collections.Generic, it still doesn't have 'ToList()' method.


You need to reference the System.Core assembly in your project, and add using statements for System.Linq and System.Collections.Generic to your code file.

ToList() is required by the IEnumerable<T> interface (which is defined in System.Collections.Generic); therefore, any type that implements IEnumerable<T> will have an implementation of ToList().  This is true of our EntityQuery<T>. However, as you apply extension methods to a query, most such methods return a new query (using the prior one as input), and sometimes the return type can change.  But anything that's an IEnumerable<T> (this includes EntityQuery<T>, IEntityQuery<T>, and IQueryable<T>) should provide ToList().

Note that under certain circumstances you can get the non-generic IEnumerable instead of IEnumerable<T>. In those cases you may be able to call the Cast() method on the IEnumerable to cast it to an IEnumerable<T>, which will have ToList().

Back to Top
 Post Reply Post Reply

Forum Jump Forum Permissions View Drop Down