locked
Computed property with field from related entity RRS feed

  • Question

  • This has to be really simple but:

    I have two entities:

    Item
    Name (the entity summary property)
    Description
    StockLocation (related, not required)

    StockLocation
    Row
    Shelf
    Box
    Position
    Item
    CurrentItem (calculated field, should show the Summary property from the Item entity, if any)

    I want to calculate the StockLocation.CurrentItem field as a string after checking for null so the Item in that StockLocation will show in a grid of StockLocations. I know that I can use related entities in a computed field but I must be calling them incorrectly... (in c#)

    Thanks


    4tuneate

    Sunday, September 30, 2012 5:59 AM

Answers

  • You could do something like this:

    partial void CurrentItem_Compute(ref string result)
    {
        if (null == this.Item)
            result = null;
        else
            result = this.Item.ToString();
    }

    Calling ToString() on an entity instance will return the string value of the summary property for that entity.

    If you objective is just to get the summary property value of the Item instance to show in the data grid row of the StockLocation instance, then you can just add the Item property as another column in the data grid and change its control type of be a Summary control. Summary controls will show the summary property value of the entity instance to which they are bound. If you do it this way, you won't need to create the above computed property.


    Justin Anderson, LightSwitch Development Team

    • Proposed as answer by freddyccix Sunday, September 30, 2012 1:48 PM
    • Marked as answer by 4tuneate Monday, October 1, 2012 12:07 AM
    Sunday, September 30, 2012 8:21 AM
    Moderator

All replies

  • You could do something like this:

    partial void CurrentItem_Compute(ref string result)
    {
        if (null == this.Item)
            result = null;
        else
            result = this.Item.ToString();
    }

    Calling ToString() on an entity instance will return the string value of the summary property for that entity.

    If you objective is just to get the summary property value of the Item instance to show in the data grid row of the StockLocation instance, then you can just add the Item property as another column in the data grid and change its control type of be a Summary control. Summary controls will show the summary property value of the entity instance to which they are bound. If you do it this way, you won't need to create the above computed property.


    Justin Anderson, LightSwitch Development Team

    • Proposed as answer by freddyccix Sunday, September 30, 2012 1:48 PM
    • Marked as answer by 4tuneate Monday, October 1, 2012 12:07 AM
    Sunday, September 30, 2012 8:21 AM
    Moderator
  • Thank you for the reply. I tried both methods.

    If I add the Items entity via the "Add Items" link in the screen designer and then drag it to the grid it will only display as a grid, not a summary property.

    If I add the Item entity as a local property, I do not see a way to bind Item to the individual rows of Stock Location. 

    If I add Items (Single or Default) via a query then bind its Id to StockLocations.SelectedItem.Id, I see the same Item in every row of the StockLocations grid.

    If I add Items (All) I get a data grid as the only option to add to the StockLocations grid.

    If I use the code you provided to calculate the CurrentItem property (used Items, not Item to avoid an error), I get this as the value for CurrentItem in every row of the StockLocations grid:

    Microsoft.LightSwitch.ClientGenerated.Implementation.EntityCollectionImplementation...

    Thanks in advance for your time.


    4tuneate

    Sunday, September 30, 2012 11:07 PM
  • You'll have to better describe what you really want to accomplish because in your opening post, you described the Item property (which makes anyone who reads that think it's a single entity, not a collection) as a single entity instance, not a collection. So the CurrentItem property cannot show "the" summary property of Item "entity" because it's not just one entity, it's a collection. Maybe you actually meant to have the a 0..1 to 1 relationship between StockLocation and Item (a 'StockLocation' must have a 'Item'; a 'Item' can have one 'StockLocation')?

    Justin Anderson, LightSwitch Development Team

    Sunday, September 30, 2012 11:18 PM
    Moderator
  • Thank you for responding so quickly. Sorry that I was not using clear terminology. There are two entity sets, Item and StockLocation and the properties listed should have been indented to make it clear. You pointed me in the right direction by questioning my relationship settings. The computed property code is now doing the trick. Thank you for all of the help you provide on the LS forums. You have saved others countless hours of time by sharing your expertise. :-)

    4tuneate

    Monday, October 1, 2012 12:07 AM
  • Don't "add the Items entity via the "Add Items" link in the screen designer and then drag it to the grid". Thet will give you a related collection. When you grag a collection to the control tree, you get a DataGrid, which is not what you want.

    Instead, just drag the navigation property (that was added when you created the relationship) to the control tree & you'll get an ACB (AutoCompleteBox), which as Justin described, you can change the control type to Summary.

    Another way to get extra information displayed is by using the "Add Other Screen Data", then pointing it to the navigation property.

    Then you specify the related entity, & property, to display. Intellisense will be useful here, when you type a period, to show you what entites/properties are available.


    Yann - LightSwitch Central - Click here for FREE Themes, Controls, Types and Commands
     
    If you find a reply helpful, please click "Vote as Helpful", if a reply answers your question, please click "Mark as Answer"
     
    By doing this you'll help people find answers faster.

    Monday, October 1, 2012 4:40 AM
    Moderator
  • how i got it in vb.net

    thanks

    Saturday, May 17, 2014 8:16 AM