Print Page | Close Window

filtering related collections

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=3221
Printed Date: 13-May-2026 at 11:36am


Topic: filtering related collections
Posted By: voulgeor
Subject: filtering related collections
Date Posted: 18-Jan-2012 at 9:11am
hi i have an entity called  item
like this

itemID
itemCode
itemPrice

there is a related  entity called ItemDescriptions
like this

itemID
Description
LanguageId


when i run the query 
dim k=from t in manager.item where t.itemid=15 select t
i get correctly 
the item which has id 15
but in the k.ItemDescriptions collection i get all the six description of the item 15  for the 6 different languages (correctly again).
is there a way to form the query in such a way to filter  that collection so it has  only one description that with languageid=20?

thanks




Replies:
Posted By: sbelini
Date Posted: 18-Jan-2012 at 3:56pm
Hi voulgeor,
 
In you case, you might want to retrieve the ItemDescription with languageid=20 manually and set the EntityReferenceLoadStrategy to DoNotLoad.
 
You can find more information about Navigation Properties and EntityReferenceStrategy in the http://drc.ideablade.com/xwiki/bin/view/Documentation/navigation-properties-data-retrieval - DevForce Resource Center .
 
Silvio.


Posted By: voulgeor
Date Posted: 19-Jan-2012 at 2:38am
i am sorry i didnt get that ..
the languageid depends on  the customer that the order goes
it changes the description of the item according to customer checking the item.

i am new to devforce can you provide me with an example as i did?

thanks again and sorry 


Posted By: sbelini
Date Posted: 19-Jan-2012 at 3:49am

Hi voulgeor,

No problem. You can set the DefaultEntityReferenceStrategy for the EntityManager:

manager.DefaultEntityReferenceStrategy = New EntityReferenceStrategy(EntityReferenceLoadStrategy.DoNotLoad, MergeStrategy.OverwriteChanges)

 

or for the EntityType:

Item.PropertyMetadata.ItemDescriptions.ReferenceStrategy = New EntityReferenceStrategy(EntityReferenceLoadStrategy.DoNotLoad, MergeStrategy.OverwriteChanges)

 

or even for the given entity only: (by setting GetEntityReference.IsLoaded to true)

Item.PropertyMetadata.ItemDescriptions.GetEntityReference(item).IsLoaded = True

 

Then can still use your same query to retrieve item and once you have it, you can query for the itemdescription given the languageid criteria.

This way you can navigate to Item.ItemDescriptions and will only retrieve the itemdescription matching the languageid given (since it's the only one in cash and we are not accessing the datasource)

Another way, maybe simpler, would be to filter the navigation property as you retrieve it:

Dim itemdescriptions = item.ItemDescriptions.Where(Function(id) id.LanguageId == languageid)

 

Regards,

   Silvio.

 
 


Posted By: voulgeor
Date Posted: 20-Jan-2012 at 6:07am
it doesnt work for me...

i have used 
	    SaleItem.PropertyMetadata.SaleItemslanguages.ReferenceStrategy = New EntityReferenceStrategy(EntityReferenceLoadStrategy.DoNotLoad, MergeStrategy.OverwriteChanges)
 
            Dim kk = (From t In ctx.SaleItemslanguages Where t.LanguagesID = 20).ToList
 
            Dim k = (From t In ctx.SaleItems.Include("SaleItemslanguages"Where t.SaleItemsID = 15 Select t).ToList
the k.SaleItemslanguages
again contains 6 rows...

how should i fill the SaleItemslanguages collection so it will be used as described in the cache
what am i doing wrong?
i am new to this.
thanks for your help 




Posted By: voulgeor
Date Posted: 23-Jan-2012 at 8:44am
an example of the procedure 
so it works would be nice thanks



Posted By: sbelini
Date Posted: 23-Jan-2012 at 9:27am
Hi voulgeor,
 
You should not have the Include statement in your query as it will force a trip to the datasource:
 
Dim k = (From t In ctx.SaleItems.Include("SaleItemslanguages") Where t.SaleItemsID = 15 Select t).ToList
Regards,
   Silvio.


Posted By: voulgeor
Date Posted: 23-Jan-2012 at 11:13am
thanks for your help

now after doing that in my wpf form
the ui bind to the saleitems works fine 
but the listbox that is bind to saleitems.SaleItemslanguages
shows nothing.

iam just seting the binding contex to the selected saleitem.
the navigation to SaleItemslanguages finds nothing to return
if i remark the 
  SaleItem.PropertyMetadata.SaleItemslanguages.ReferenceStrategy = New EntityReferenceStrategy(EntityReferenceLoadStrategy.DoNotLoad, MergeStrategy.OverwriteChanges)
it works as expected (because i guess goes back to the sql server)
but again shows all the values and not the filtered ones.





Posted By: sbelini
Date Posted: 23-Jan-2012 at 1:07pm
Hi voulgeor,
 
I'm confused.
 
The listbox binding to salesitems.SaleItemsLanguages should show only one element (the same as in k.SaleItemslanguages)
 
If you need all salesitems in cache to be able to populate your listbox, you will not be able to bind the your ui directly to the navigation property (i.e. k.SaleItemslanguages).
You are doing your binding in the XAML, right? Try instead doing the binding on code:
i.e.
mySalesItemsLanguagesGrid.ItemsSource = someSaleItem.ItemDescriptions.Where(Function(id) id.LanguageId == languageid)
 
Silvio.


Posted By: voulgeor
Date Posted: 23-Jan-2012 at 2:41pm
no each time 
i display one saleitem only
the listbox displays the 
saleitem.SaleItemslanguages
as you said
but the problem is that 
it doesnt have any items to be displayed.
it doesnt display any elements if
i have 
 SaleItem.PropertyMetadata.SaleItemslanguages.ReferenceStrategy = New EntityReferenceStrategy(EntityReferenceLoadStrategy.DoNotLoad, MergeStrategy.OverwriteChanges)
and fill the SaleItemslanguages with the filtered query
the navigated k.SaleItemslanguages is empty (should have only one element).
it should display the one filtered description isnt?
BUT if i put in remark
the  SaleItem.PropertyMetadata.SaleItemslanguages.ReferenceStrategy = New EntityReferenceStrategy(EntityReferenceLoadStrategy.DoNotLoad, MergeStrategy.OverwriteChanges)
then it displays correctly (i suppose because it goes to the sql database to fetch the values)
but as expected all the 6 values for the selected saleitem show up (not filtered).

if the referencestrategy is set DoNotLoad it doesnt go to the database (correctly)
but also  doesnt  navigate to the cached entity that we load manualy previously (to return that one element)










Posted By: sbelini
Date Posted: 23-Jan-2012 at 4:30pm
Can you create a simple test solution (against NorthwindIB please) demonstrating your issue and upload it here?
 
Thanks,
   Silvio.



Print Page | Close Window