views:

255

answers:

2

Hi All,

How can i create a query that will return a single entity along with a collection, and have the collection sorted? i don't care if it's two calls to the DB.. i just need the end result to be an Entity type and not an anonymous type:

var

category = context.Categories.Where(c => c.categoryID == categoryID).Select(c => new { c, products = c.Products.OrderByDescending(p => p.priceDate) }).First();

But i'd like to have the above either return or cast to a Category with the Products collections.. as apposed to anonymous type. thanks!

A: 

If you have the correct foreign key relationships in your database then doing a simple select for your category should suffice and then you can order the products so

Category category = context.Categories.Where(c => c.categoryID == categoryID).First();

List<Product> sortedProducts= context.Categories.products.OrderBy(...).ToList()

When your FK relationships are set up when you get the top level object it should retrieve its children too (i havent worked with linq-to-entities but with linq-to-sql i cant imagine it being diffrent in this aspect tho) I think the above should work...

Daniel
Hi thanks for the response. This definitely seems like it should work.. and seems so obvious now! But i'm getting:"Cannot implicitly convert type "System.Collections.GenericList<MyEntities.Products> to System.Data.Objects.DataClasses.EntityCollection<MyEntities.Products>"
radmna
Yea, thats cuz you cant .ToList() i dont think, try not putting .ToList() there, or try using sort() so you dont ahve to assign it
Daniel
Without toList() there wouldn't be a result. there is no sort..Until the 2nd statement the Products are not loaded...
radmna
right, so the problem is there isnt a .ToEntitySet() method so i would suggest go with the answer below me, add the porperty to the partical Category class...that will work well i think
Daniel
Or check out my edit, you can just work with the list of product, they have a back reference to the category, so you would be a ble to access to category as well
Daniel
I guess the issue is the entity model is perfect.. related objects and collections.. everything is in there already. My query is actually a little more complicated as i need to include 2 other related objects along with the category. I guess i can just split the query into two seperate results. I am working in a 3 tier environment and would be great to just return the strongly type entity as defined in the model.. I'm just suprised that it can't be done.. what's the purpose of the entitycollection?
radmna
Its just a way that linq does its relationships...In my opinion linq objects should be used to write and get info from database, to use in your app as an object I would recommend making a Domain Object that you translate your linq objects to...
Daniel
Well i think that's what linq-to-entities is all about. Otherwise why bother generating an entity model? Anyway thanks for your input. Ultimately made two seperate methods in my BL.. one to return the category and related objects and one to return the sorted objects. Works fine. thanks!
radmna
A: 

Could you just put that into the category class? Something like this:

public parital class Category 
{
        public Ilist<Product> ProductsByPriceDate
        {
          get
          {
              return this.Products.OrderbyDescending(p= > p.priceDate).ToList();
          }
        }
    }
Hath