Entity Framework: returning the Entity with the Min() cost

Discussion in 'Microsoft ADO .NET' started by Markla, Sep 4, 2009.

  1. Markla

    Markla Guest

    Hi,

    I'm trying to find an efficient way to search for entity(s) with the lowest
    cost from a table of about 2,000 items. Rather than loading all 2,000 items,
    which I don't otherwise need, I'm wanting to use an ObjectQuery to return
    just the one(s) I need.

    I can see a 2-step way of doing this, but want to do it in just one call.

    Assuming a table called 'Items', with fields ID, Name, Type and Cost:

    float minCost = objectContext.Items.Where(i=>i.Type ==
    "SmallType").Min(i=>i.Cost).FirstOrDefault();

    ObjectQuery<Item> items = objectContext.Items.Where(z=>z.Cost == minCost);

    Is there a way to do this which results in just one call to the database?

    Thanks.
    +M
     
    Markla, Sep 4, 2009
    #1
    1. Advertisements

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Guest

    Entity Framework

    Guest, Aug 29, 2007, in forum: Microsoft ADO .NET
    Replies:
    0
    Views:
    315
    Guest
    Aug 29, 2007
  2. Alexander Vasilevsky

    When will ADO.NET Entity Framework?

    Alexander Vasilevsky, Jan 21, 2008, in forum: Microsoft ADO .NET
    Replies:
    2
    Views:
    297
    Mary Chipman [MSFT]
    Jan 22, 2008
  3. knorth
    Replies:
    0
    Views:
    434
    knorth
    Apr 6, 2008
  4. doph

    ado.net entity framework newby question

    doph, Apr 23, 2008, in forum: Microsoft ADO .NET
    Replies:
    1
    Views:
    279
    Miha Markic
    Apr 24, 2008
  5. Fakher Halim
    Replies:
    0
    Views:
    323
    Fakher Halim
    Jul 17, 2008
  6. Greg

    ADO.NET Entity Framework

    Greg, Aug 5, 2008, in forum: Microsoft ADO .NET
    Replies:
    0
    Views:
    290
  7. H5N1
    Replies:
    5
    Views:
    968
  8. CGatto
    Replies:
    1
    Views:
    3,624
    CGatto
    Aug 7, 2009
Loading...