locked
Simple Lookup to Related Entity into Computed Field RRS feed

  • Question

  • I have been looking for a sample VB code for Lookup Validation in a computed field at Entity Level and wondered if anyone has an example.

    I have 2 tables
    PRODUCTS (mutliple produc records with a location code and computed description)
    LOCATIONS (list of codes and descriptions)

    All I want to do is have the computed description on the PRODUCT Entity so it is available when I open PRODUCT Screens and when I select a new location code on any PRODUCT screen.

    I hope this makes sense.

    Friday, October 5, 2012 9:33 AM

Answers

  • Thanks very much for your input, I will review in more detail later I just came backm on to say I realised what I was doing wrong.

    Although I had created a relation I had not realised it created another entry in the entity for the collection.

    So I was trying to use the actual column rather than the new entry Light Switch added to my entry, at least I think that was it - it all works as I expected now but it looks like I have a bir more reading to do when I return from the day job!

    May thanks everyone.
    • Proposed as answer by Yann DuranModerator Thursday, October 11, 2012 4:40 AM
    • Marked as answer by Otomii Lu Thursday, October 18, 2012 1:50 AM
    Wednesday, October 10, 2012 9:58 AM

All replies

  • If the two tables are related with navigation property then you don't need a lookup validation.

    If the two tables are not related then you need to create computed field with the result code that looks like below:

    result = (From l In LOCATIONS Where l.LocationCode=Me.LocationCode).FirstOrDefault.Description


    "HARD WORK BEATS TALENT WHEN TALENT DOESN'T WORK HARD"

    HAROLD GLENN MINERVA
    http://hgminerva.wordpress.com

    If you found this post helpful, please “Vote as Helpful”. If it answered your question, please “Mark as Answer”. This will help other users to find their answer quickly.

    Friday, October 5, 2012 2:33 PM
  • No Sorry, I am still stuck I did not explain in enough detail.

    PRODUCTS and LOCATIONS are related by a common Stock_Location_Code column in each table.

    I have a description column on the LOCATION entity and I want it to bring this values into a  computed column I have added on the PRODUCTS entity when the Stock_Location_Code matches.

    Sorry I am very new to LS, I haven't yet written a single bit of code although I am confident with TSQL

    Thanks for your time

    Monday, October 8, 2012 2:07 PM
  • I don't follow.

    What's the relationship between PRODUCTS and LOCATIONS?
    Can you give us an example of what the data looks like, and what you want the output to look like?


    Free Visual Studio LightSwitch extensions: Elyl's Extensions http://visualstudiogallery.msdn.microsoft.com/bbe013bf-45b6-46c4-ba13-537cc23c5118

    Monday, October 8, 2012 2:42 PM
  • I am not sure how else to explain it , I didn't want to post the actual screens as I simplified it for the question. below is the relation with the real entity names, but the image below that is how I would explain it in simple terms, the two table names at the top and the red box being the computed data.

    I want to be able to Select a Status code on the product and validate it is an entry in the STOCK_STATUS_CODE table and also lookup the description at the same time.

    Monday, October 8, 2012 6:24 PM
  • From what you are generally saying, I suspect what you want is easy. However, I cannot fathom out from your description exactly what you want. I suspect that stands for others too, sorry.

    If you can give the relevant info as regards properties and relationships for the 4 entities it would help:

    PRODUCT, LOCATION, ASSET, STOCK_STATUS_CODE

    One thing from your description sounds like there should be a relationship between PRODUCT and STOCK_STATUS_CODE, if not why not?

    Thanks


    Dave Baker | Xpert360 blog | twitter : @xpert360 Opinions are my own. Please mark as answer if this helps solve your problem.

    Monday, October 8, 2012 7:33 PM
  • Yes I totally confused everyone (Sorry), there are only 2 entities, I tried to simplify my example for the forum.

    2 Tables (ASSET,STOCK_STATUS_CODES)
    2 Columns in each Table (Status_Code & Description) - Description Computed in ASSET
    1 Relation on the Status_Code

    Example Contents in STOCK_STATUS_CODES
    OK - Asset OK
    NOK - Not OK
    P - Passed
    F - Failed

    So the idea is that the users can type or pick a code in the ASSET screen and it will pull in the description for it in the Description field.

    Perhaps I am over-complicating things having 2 columns for the pick list, our old system used this to be able to allow the users a quick keyboard entry to the code if they knew it, I guess really I should just be creating a simple picklist ?

    (ASSET.Status_Code,ASSET.Stock_Status_Desc (computed) )


    2 Common Codes

    Wednesday, October 10, 2012 7:03 AM
  • You could simply use a choice list for a property called StatusCode on the Asset entity.

    You could simply dispense with the Asset.StockStatusDesc and use a bit of screen design:

    Or you could use a calculated property with code like this:

    namespace LightSwitchApplication
    {
        public partial class Asset
        {
            partial void StockStatusDesc_Compute(ref string result)
            {
                if (this != null & StockStatusCode != null)
                    result = StockStatusCode.Description;
    
            }
        }
    }

    Which ends up looking like this...

    Nevertheless its your choice.


    Dave Baker | Xpert360 blog | twitter : @xpert360 Opinions are my own. Please mark as answer if this helps solve your problem.


    • Edited by Xpert360 Wednesday, October 10, 2012 8:47 AM
    • Proposed as answer by Xpert360 Wednesday, October 10, 2012 8:48 AM
    Wednesday, October 10, 2012 8:46 AM
  • They could pick the code and have the computed value displayed next to it, that's easy enough to do.  For example, on your ASSET screen, have an autocomplete box to select the status code (bound to status code) and next to it have a label bound to status description.  Then when the user picked a status code, they'd see the status code and the description next to it.

    You might even want to set the Summary Property of your STATUS_CODES table to be the computed value, in which case you could have the autocomplete box just show the description, so the user needn't have to see the status code at all.


    Free Visual Studio LightSwitch extensions: Elyl's Extensions http://visualstudiogallery.msdn.microsoft.com/bbe013bf-45b6-46c4-ba13-537cc23c5118

    Wednesday, October 10, 2012 8:46 AM
  • Thanks very much for your input, I will review in more detail later I just came backm on to say I realised what I was doing wrong.

    Although I had created a relation I had not realised it created another entry in the entity for the collection.

    So I was trying to use the actual column rather than the new entry Light Switch added to my entry, at least I think that was it - it all works as I expected now but it looks like I have a bir more reading to do when I return from the day job!

    May thanks everyone.
    • Proposed as answer by Yann DuranModerator Thursday, October 11, 2012 4:40 AM
    • Marked as answer by Otomii Lu Thursday, October 18, 2012 1:50 AM
    Wednesday, October 10, 2012 9:58 AM