This is a migrated thread and some comments may be shown as answers.

Filter entries of inverse list

3 Answers 79 Views
LINQ (LINQ specific questions)
This is a migrated thread and some comments may be shown as answers.
This question is locked. New answers and comments are not allowed.
kor
Top achievements
Rank 1
kor asked on 22 Jan 2010, 02:41 PM

I have a master - detail relationship (folders with items) and have defined the inverse list of details in the master object in the mapping editor. So I get all the detail objects when I read a master object with a deep fetch and the detail objects have their references set by the ORM, too.
Now I want to filter the details depending on the user's role on the server before they are sent to the client over the wire. The details collection of the master object should just contain those items which meet a condition.

How can I achieve this? Can I do this in one query? How would a LINQ query look like?

(I'm working with the 2009.1 version.)
 

Thank you

Rupert

3 Answers, 1 is accepted

Sort by
0
PetarP
Telerik team
answered on 27 Jan 2010, 05:39 PM
Hello kor,

Unfortunately this is not possible with the 2009 Q1 release. I am afraid that back in the beginning of 2009 our LINQ support was not so well developed. However we have implemented a lot of improvements that can be found in the Q3 2009 version. One of these improvements is the ability to filter collections in LINQ queries. Having that in mind it wont be possible to achieve your goal using Q1. A possible solution would be to use code similar to this:
var result = from c in scope.Extent<Order>().ToList()
             from f in c.OrderDetails
             where f.ProductID==1
             select c;
This would filter all Order objects which contain a details with productID 1. The catch here is that ToList() would return all orders and they will be filtered on the client side by using LINQ to objects.

Greetings,
Petar
the Telerik team

Instantly find answers to your questions on the new Telerik Support Portal.
Watch a video on how to optimize your support resource searches and check out more tips on the blogs.
0
kor
Top achievements
Rank 1
answered on 01 Feb 2010, 01:24 PM

(Maybe I should have posted my question in another forum, e.g. Getting Started.)


Hello Petar,

using OQL instead of LINQ would be ok. The question is about setting a filter on an inverse list.

My Folder class has an unmanaged list of items defined in the config by an element like

 <field name="items">
  <collection>
   <extension key="inverse" value="folder" />
  </collection>
 </field>

Now I have the following object structure:

 

 Folder_1

   Item_1

   Item_2

   Item_3

i.e. Item_1, Item_2, Item_3 are referencing Folder_1.
 

When I read the Folder_1 folder the items list is populated by the ORM with the 3 Item objects. But in some cases the items list should just contain a subset of these items, e.g. Item_3 or {Item_1, Item_2} depending on a filter predicate.

My question is: Can I achieve that using an inverse list defined like above or should I define the collection outside the ORM and populate it in the program?

Regards
Rupert

PS: If my post doesn't fit into this forum group, feel free to move it to a more suitable. 

 

 

 

 

 

0
PetarP
Telerik team
answered on 03 Feb 2010, 01:21 PM
Hello kor,

The only way to achieve a similar functionality is to take advantage of the IInstanceCallbacks interface. You can refer to this knowledge base article for more information. Basically what you need to do is implement some custom logic in your PostLoad method that will be filtering your collection. Please note that this method will be fired after the collection has been retrieved thus the actual filtering will be performed on the client side. Here is an example how you can filter the OrderDetails collection of an Order object:
bool needsToCommit = false;
            List<OrderDetail> newCollection = this.OrderDetails.Where(x => x.Quantity < 11).ToList();
            if (!Database.GetContext(this).Transaction.IsActive)
            {
                needsToCommit = true;
                Database.GetContext(this).Transaction.Begin();
            }
            this.orderDetail.Clear();
            foreach (OrderDetail dt in newCollection)
            {
                this.orderDetail.Add(dt);
            }
            if (needsToCommit)
                Database.GetContext(this).Transaction.Commit();


Greetings,
Petar
the Telerik team

Watch a video on how to optimize your support resource searches and check out more tips on the blogs.
Follow the status of features or bugs in PITS and vote for them to affect their priority.
Tags
LINQ (LINQ specific questions)
Asked by
kor
Top achievements
Rank 1
Answers by
PetarP
Telerik team
kor
Top achievements
Rank 1
Share this question
or