New Posts New Posts RSS Feed: POCO Query Question
  FAQ FAQ  Forum Search   Calendar   Register Register  Login Login

POCO Query Question

 Post Reply Post Reply
Author
ken.nelson View Drop Down
Groupie
Groupie


Joined: 03-Mar-2009
Location: Ann Arbor, MI
Posts: 54
Post Options Post Options   Quote ken.nelson Quote  Post ReplyReply Direct Link To This Post Topic: POCO Query Question
    Posted: 02-Dec-2010 at 5:34pm
Is there any way to perform the following in one query?  Something like...
 
em.Customers
     .Select(c => new CustomerLite()
     {
          CustomerID = c.ID,
          CustomerName = c.Name,
          ... etc ...
 
          OrderCount = c.Orders.Count(),
          Orders = c.Orders
               .Select(o => new OrderLite()
               {
                     OrderID = o.ID,
                     OrderDate = o.Date,
                     OrderProductCount = o.Products.Count,
                     ... etc ...
               })
     });
 
CustomerLite and OrderLite are POCO entities and are intended to be lighter-weight versions of Customer and Order.  I've tried countless ways of getting this (or similar) to work and I'm fairly convinced it's not possible.  Everything works without issue until I attempt to pull the collection of OrderLite.  I'm at the point that the only thing I can think to do is pull the CustomerLites and OrderLites in separate queries and add the OrderLites to the CustomerLites manually.  Am I missing something obvious? 
 


Edited by ken.nelson - 03-Dec-2010 at 6:17am
Back to Top
sbelini View Drop Down
IdeaBlade
IdeaBlade
Avatar

Joined: 13-Aug-2010
Location: Oakland
Posts: 786
Post Options Post Options   Quote sbelini Quote  Post ReplyReply Direct Link To This Post Posted: 07-Dec-2010 at 1:02am

Hi ken.nelson,

You should be able to execute the query you provided with no issues.
I've tried it here and had no problems.
What's the error you are getting? How are you implementing your POCO types? Are you using the DataContract and DataMember flags? (as in the example below)
 
// CustomerLite POCO Entity
[DataContract]
public class CustomerLite {
  [Key]
  [DataMember]
  public int CustomerID {
    get;
    set;
  }
 
  [DataMember]
  public string CustomerName {
    get;
    set;
  }
 
  [DataMember]
  public IEnumerable<OrderLite> OrderLites {
    get;
    set;
  }
 
// OrderLite POCO Entity
[DataContract]
public class OrderLite {
  [Key]
  [DataMember]
  public int OrderID {
    get;
    set;
  }
 
  [DataMember]
  public decimal? Freight {
    get;
    set;
  }
 
  // Query executed with no issues
  var query = _mgr.Customers
    .Select(c => new CustomerLite() {
      CustomerID = (int)c.Id,
      CustomerName = c.ContactName,
      OrderLites = c.OrderSummaries
      .Select(o => new OrderLite() {
        OrderID = (int)o.Id,
        Freight = o.Freight,
      })
    });
 
 
sbelini


Edited by sbelini - 07-Dec-2010 at 1:08am
Back to Top
 Post Reply Post Reply

Forum Jump Forum Permissions View Drop Down