locked
Compute a property from querying other table RRS feed

  • Question

  • I have two tables:

         EmployeeData(lname,grade,midpoint)

         GradeInfo(grade, median)

    I would like to make the midpoint property in the EmployeeData table a computed property that displays the median value from the GradeInfo table based on a match on grade between the two tables.  Nothing I have tried is successful thus far.  I am pretty new to LS and thought I could use a LINQ query in c# with this.DataWorkSpace but I cannot seem to get the syntax right.  Is this the proper way to do this or is there something else that would work better?

    Any help is greatly appreciated.

    Thursday, December 20, 2012 5:23 AM

Answers

  • Hello

    There is a lot of guesswork here but this is what I would try. I am assuming that midpoint is a computed column on your EmployeeData Database and that GradeInfo is in the same DataWorkspace.

    partial void midpoint_Compute(ref int result)
    {
    
    	int intTmp = 0; //Default value if nothing is found
    	
    
    	//the next line creates a variable of type GradeInfo table and finds the correct record
    	GradeInfoes getMedian = (from items in this.DataWorkspace.ApplicationData.GradeInfo
    				where items.grade == this.grade
    				select items).FirstOrDefault();
    
    	if(getMedian != null){	//If a record is found then set the temporary variable to the value of the median.
    		intTmp = getMedian.median;
    	}
    
    	result = intTmp;
    }

    If this is not the exact answer it should help you on your way :)

    Merry Christmas


    If you found this post helpful, please mark it as helpful. If by some chance I answered the question, please mark the question as answered. That way you will help more people like me :)

    • Proposed as answer by Yann DuranModerator Friday, December 21, 2012 2:34 AM
    • Marked as answer by tmhamlin Friday, December 21, 2012 1:43 PM
    Thursday, December 20, 2012 4:25 PM

All replies

  • Hello

    There is a lot of guesswork here but this is what I would try. I am assuming that midpoint is a computed column on your EmployeeData Database and that GradeInfo is in the same DataWorkspace.

    partial void midpoint_Compute(ref int result)
    {
    
    	int intTmp = 0; //Default value if nothing is found
    	
    
    	//the next line creates a variable of type GradeInfo table and finds the correct record
    	GradeInfoes getMedian = (from items in this.DataWorkspace.ApplicationData.GradeInfo
    				where items.grade == this.grade
    				select items).FirstOrDefault();
    
    	if(getMedian != null){	//If a record is found then set the temporary variable to the value of the median.
    		intTmp = getMedian.median;
    	}
    
    	result = intTmp;
    }

    If this is not the exact answer it should help you on your way :)

    Merry Christmas


    If you found this post helpful, please mark it as helpful. If by some chance I answered the question, please mark the question as answered. That way you will help more people like me :)

    • Proposed as answer by Yann DuranModerator Friday, December 21, 2012 2:34 AM
    • Marked as answer by tmhamlin Friday, December 21, 2012 1:43 PM
    Thursday, December 20, 2012 4:25 PM
  • @Otis,

    One small suggestion for you. It would be more correct if in your LINQ query, that you used the singular, rather than the plural, for item.

    So:

    partial void midpoint_Compute(ref int result)
    {
        int intTmp = 0; //Default value if nothing is found
    
        //the next line creates a variable of type GradeInfo table and finds the correct record
        GradeInfo getMedian = 
        (
            from item in this.DataWorkspace.ApplicationData.GradeInfos        
            where (item.grade == this.grade)
            select item
        ).FirstOrDefault();
    
        if(getMedian != null)
        {	//If a record is found then set the temporary variable to the value of the median.
            intTmp = getMedian.median;
        }
    
        result = intTmp;
    }


    I know it's a bit pedantic, but I thought I'd just throw that out there for people new to LINQ. It makes the where clause & select clause a bit more readable.

    There was also a reversal of GradeInfo & GradeInfos, which was probably just a typo.

    GradeInfos getMedian = (from item in this.DataWorkspace.ApplicationData.GradeInfo
    
    'should be
    
    GradeInfo getMedian = (from item in this.DataWorkspace.ApplicationData.GradeInfos


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



    Friday, December 21, 2012 2:42 AM
    Moderator
  • Thanks Gents!  I appreciate the assistance as it definitely helped me head in the right direction.

    Merry Christmas!

    Friday, December 21, 2012 1:44 PM
  • @tmhamiln = Glad it helped.

    @Yann = Thank you for being Pedantic. If it wasnt for your help (and a few others) I wouldnt have the experience to pass on to others in this forum.


    If you found this post helpful, please mark it as helpful. If by some chance I answered the question, please mark the question as answered. That way you will help more people like me :)

    Friday, December 21, 2012 2:19 PM
  • @Otis - You're welcome, as always. Thanks for the kind words, it's nice to know that I've helped. It's why I spend time here.

    @tmhamlin - You're very welcome as well. :-)


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

    Friday, December 21, 2012 2:54 PM
    Moderator