Print Page | Close Window

POCO Query Question

Printed From: IdeaBlade
Category: DevForce
Forum Name: DevForce 2010
Forum Discription: For .NET 4.0
URL: http://www.ideablade.com/forum/forum_posts.asp?TID=2345
Printed Date: 21-Apr-2025 at 2:23pm


Topic: POCO Query Question
Posted By: ken.nelson
Subject: POCO Query Question
Date 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? 
 



Replies:
Posted By: sbelini
Date 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



Print Page | Close Window