EF order by property of navigation property? - entity-framework

I have a query that looks like this:
db.Items.Where(c => c.Enabled)
.OrderBy(c => c.Vendor.Category.Select(b=> b.OrderPriority))
.ToList();
I'm getting an error saying I need to implement IComparable... I'm not sure that I'm doing the right thing - I just want to order my items the OrderPriority property of Category. Is there any way to order using a property of a navigation property?

Basically you are trying to order Items by a collection of OrderPrioritys. This is because a Vendor has multiple Categories (or may have, because it's a 1-n association).
I'm not sure if it is even possible what you're trying to express. If you want to order the Items by the highest OrderPriority found in one of its Vendor's categories, you could do
db.Items.Where(c=>c.Enabled)
.OrderBy(c => c.Vendor.Category.Max(b=> b.OrderPriority))

Related

Entity Framework, Computed Entity Field

Is it possible to include a computed field in an EF entity? Example, lets say I have a shop selling products, and the products are grouped into categories.
When the list of categories is displayed for editing to the administrator of the shop I wish to list the number of products in each category as part of the list.
In NHibernate I would use a formula e.g.
<property name="Products" formula="dbo.Category_NumProducts(id)" />
I can't work out if something similar is possible for EF. I know I could make a category view and map this to a second entity but that seems wrong as its almost a complete duplication.
EDIT: If this isn't possible in EF, then what is the best way to accomplish this?
Unfortunately if your Category table doesn't have this as computed column you will not be able to map it without creating second entity - it leads to database view, defining query in EDMX or perhaps Query view in EDMX (but Query view may not work because you require aggregate function to be computed).
Edit:
IMHO the simplest solution (and also the best) is simply creating a ViewModel (some non mapped class) and use projection in Linq query:
var query = from c in context.Categories
where ...
select new CategoryView {
Id = c.Id,
Name = c.Name,
...
Products = c.Products.Count()
};

Entity Framework Lazy loading and ICollection

Lets say that I have a class called Stock and which has a virtual ICollection Prices, which is a historical set of prices.
If you fetch a stock and after the stock is materialized you query the Prices but apply a filter like mystock.Prices.OrderByDescending(px => px.Date).First(), EF internally loads all the prices and then it applies the filters used, since prices could be a large collection, I would really like to see EF just load the price that matched my where criteria. Basically applying the filtering at the server end rather than client side.
Is it possible to do this?
Thanks
It's possible, but this way only works if you can assume Prices is really an EntityCollection rather than some other class that also happens to implement ICollection. I'm not sure if this is true in all supported EF scenarios. The function to use is EntityCollection's CreateSourceQuery function.
((EntityCollection<Price>)stock.Prices).CreateSourceQuery().OrderByDescending(price => price.Date).First();
If that doesn't work for you, another possibility might be to go back to the context, and query from there:
(from price in context.Prices
where price.StockId == stockId
orderby price.Date descending
select price).First();

Dynamic Form Generation with Symfony2

The scenario is pretty simple, I want to enable adding product attributes in my webhop. So, I have product table which holds product information (price, description...), attribute table which defines all possible product attributes (color, weight, power...), product_attributes table which connects this two and holds actual attribute value (red, 25kg, 51w...).
I would like to enable product form to enable adding/editing this attributes (attribute set is defined in attribute table).
I tried using collections, but that allowed me to only edit existing product attributes, not to add new.
I also experimented with event subscribers, but everything I add does not fit in my Product entity, it is just not that flexible to accept anything, it asks for attributes.
Is there any good way to solve this problem?
You can accomplish what you want with Embedding Collection of Forms, and using some jQuery magic to make it pretty.
It's explained in detail in the guide. With collections you need to use :
'allow_add' => true, 'by_reference' => false,
to allow the users to add new attributes.

Core Data - Associate a Quantity to a Relationship

I have a Core Data entity called "Item" and it represents an item in a store so it has a name, price, and a few other attributes.
I would like to be able to create lists of these items and I am having some trouble figuring out how to do it.
The problem is that I need to be able to associate a quantity for each item in the list AND I need to be able to add the item to multiple lists.
So for example, say I have an item called "Bread" and I want to add it to two different lists with different quantities associated with each relationship.
I see that the documentation for Core Data says that a userInfo dictionary can be associated with a relationship but I can't seem to locate any information that would indicate whether or not that would work for me.
Any ideas?
This is probably not the best place for a userInfo dictionary. Instead, create a new entity, which has a list releationship, an item relationship, and a quantity attribute. When you add Bread to a list, you actually add this 'link' object, and hook up the Item and List relationships, then set its quantity.

ASP.NET Dynamic Data : How to Specify Sort Order of Items in Dropdown List

I am using ASP.NET dynamic data for the data adminstration tasks for a Silverlight app that I built. It saved a ton of time not having to write all of the admin screens you typically have to create for end users to manage the data.
One thing I cannot figure out how to sort the items in the drop downs that appear on the screens - either the filter dropdowns on the list views or on the data entry screens.
Do I specify that somewhere in the EDM partial classes or in the ASP.NET DD field templates? or somewhere else?
All I need to do is sort alphabetically by the display value- they appear to be in random order.
thanks
Michael
Use the DisplayColumn Attribute in the System.ComponentModel.DataAnnotations Namespace.
http://msdn.microsoft.com/en-us/library/system.componentmodel.dataannotations.displaycolumnattribute.aspx
ex:
[DisplayColumn("LastName", "LastName")]
public partial class Employee
{
}
The answer to your question can be found here, about halfway down the page:
http://csharpbits.notaclue.net/2008/08/dynamic-data-and-field-templates-second.html
In the Cascase.ascx.cd FilterControl and Cascade_Edit.ascx.cs FieldTemplate you will find a method GetChildListFilteredByParent. This returns the values for the filtered DropDownList, but as you will see this list is an unordered list. To add sorting to this list we need to add a Linq OrderBy clause.