Filter entries of inverse list

4 posts, 0 answers
  1. kor
    kor avatar
    3 posts
    Member since:
    Nov 2009

    Posted 22 Jan 2010 Link to this post

    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

  2. PetarP
    Admin
    PetarP avatar
    754 posts

    Posted 27 Jan 2010 Link to this post

    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.
  3. DevCraft banner
  4. kor
    kor avatar
    3 posts
    Member since:
    Nov 2009

    Posted 01 Feb 2010 Link to this post

    (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. 

     

     

     

     

     

  5. PetarP
    Admin
    PetarP avatar
    754 posts

    Posted 03 Feb 2010 Link to this post

    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.
Back to Top