开发者

Storing attribute/category sorting for each profile

开发者 https://www.devze.com 2023-02-13 14:59 出处:网络
I\'m trying to figure out how to save my sorting in two levels. My problem is the following: I have a table with Profiles.

I'm trying to figure out how to save my sorting in two levels. My problem is the following:

I have a table with Profiles. Each Profile has a bunch of Attributes associated to it. The Attributes are shared among the Profiles (Many-to-Many). Each Attribute belongs to a Category.

Unfortunately I can't post an image of my model, need another 9 reputaion points...

What I want to do is group Attributes by Category and, 开发者_运维技巧for each Profile, save the order of the Categories and Attributes.

Example:

Platform: Windows Mac

Role: Architect Developer Project leader

So here I would like to be able to, for example, change the order of the platforms or have the "role" show up before "Platform".

I was thinking of adding a seperate table and just saving an array of the CategoryID:s and AttributeID:s for each Profile.. But I'm wondering if there is a better way...


Ok, let's try again :)

I assume you mean you're trying to preserve the order of entities in a many-to-many relationship?

If so, it seems this isn't possible with a stadard EF many-to-many join. Instead, you'll need to add an additional entity in the middle, which stores the sequence. This isn't elegant, but it's likely the only way.

Here's some info from Rowan Miller @ MS:

In EF a true many to many (i.e. without the join entity) can not contain any payload and the contents are not ordered.

Adding a join entity is the best way to achieve what you are after.

Source: http://social.msdn.microsoft.com/Forums/en/adonetefx/thread/6fe9f4fc-9979-4885-ad15-8c5ddebb45b1


Do you mean you want to order the child objects within each object?

If so, there's a little info that might help here (though it looks nasty):

http://weblogs.asp.net/zeeshanhirani/archive/2008/07/28/sorting-child-collections-in-entity-framework.aspx

I made a blog post earlier, where I discussed how to use AssociateWith method on DataLoadOptions to sort child collections on SQL server before they are loaded into memory. This option is only available in linq to SQL. In entity framework, in order to accomplish sorting on child collection, you have to use CreateSourceQuery method. CreateSourceQuery returns an objectquery that gets converted to SQL and send to the database. So if you want your child collections to be loaded differently such as sorted in a different order or apply filter, than you have to get access to ObjectQuery instance that is responsible for loading the child subcollection and modify the query to add ordering before ObjectQuery gets executed. CreateSourceQuery query method is available on EntityCollection and EntityReference.

Below is an example that shows how to do that

var context = new NorthwindEntities();
var cus1 = context.Customers.Single(c => c.CustomerID == "ALFKI");
foreach (var order in cus1.Orders.CreateSourceQuery().OrderBy(o => o.ShipCity)
{
       Console.WriteLine(order.ShipCity);
}
0

精彩评论

暂无评论...
验证码 换一张
取 消