locked
html client get related collection in one to many relationship by query RRS feed

  • Question

  • Hi

    I have two tables (one to many relationship). Research and ResearchAttachment. Each Research contains more than one specific ResearchAttachment. when I tried to get the Research in html client, I can only get Research data in the first click. How Can I get the ResearchAttachment at the same time ?

    my problem is when I get the Research query result , the ResearchAttachment.array.lenght is 0. But when I do the query again. the ResearchAttachment.array.lenght is 2 . why this happened.

    what's more, I have browse the "Edit Query_>Property_>ManageContainedData" there is nothing at all. 

    here's the code

    /**
     * Created by chopic on 2014/11/5.
     */
    screen.reasearchIDs = IDs;//excute the query
    
    //when the query returns the selected Researches,do OrganizeByYear
    screen.Researches.load().then(
        function (promiseItem) {
            OrganizeByYear(screen.Researches);
        });
    
    
    function OrganizeByYear(researches) {
        var attachHashSet = [];
        for (var i = 0; i < researches.count; ++i) {
            var resItem = researches.data[i];
    
            if (resItem.ResearchAttachments) {
                //the resItem.ResearchAttachments.array.length == 0 when I first click the query button
                //however it works great after the  first query
                for (var j = 0; j < resItem.ResearchAttachments.array.length; ++j) {
                    var attItem = resItem.ResearchAttachments.array[j];
                    attachHashSet[attItem.Year] = attItem;
                }
            }
        }
    }
    

    Wednesday, November 5, 2014 9:32 AM

All replies

  • actually, I want to know how can not get the related collection in one to many relationship in html client. please tell me how.
    Wednesday, November 5, 2014 11:30 AM
  • There is no need to use such complex code which is easy to get exceptions.

    Please check below article for a easy way

    http://rostacik.net/2012/01/07/handling-many-to-many-n-to-m-relationships-in-microsoft-lightswitch/

    Thursday, November 6, 2014 2:28 AM
  • thanks very much for you reply.Unfortunately, I can not get you.I just want to use the data I queried like the object I use in C#.

    for example, Research{name, ResearchAttachment(list)},why can not I get ResearchAttachment list in this way:Research.ResearchAttachment in html client.

    Could you take some time to write the key code for me?

    Thanks a lot!!!

    Thursday, November 6, 2014 7:42 AM
    1. Typo?
    2. .load() actually executes the query, which is slightly different from the comments above.
    3. promiseItem holds the results of the query returned as a successful promise.  You would want to pass this to the function organizeByYear(promiseItem), not screen.Researchers, which is the Visual Collection of the screen that LightSwitch loads separately.  screen.Researchers is not available the first time the query runs because LightSwitch is still loading it, which is why your array length is zero initially.
    4. Because arrays in JS are zero-based, for-loops are initialized at i=0 and incremented i++ to iterate from the start.

    ResearchAttachment entities are lazy-loaded when you use the screen.Researches.load() method.  If you want to eager-load both the Researches entity set and corresponding ResearchAttachments, you can use a slightly different query syntax:

        myapp.activeDataWorkspace.ApplicationData.Researches
            .expand('ResearchAttachments')
            .execute()
            .then(organizeByYear(promiseItem));
    
    promiseItem should hold an array of Researches with each Research entity having an array of ResearchAttachment entities.

    Thursday, November 6, 2014 7:43 PM
  • hi LittleBobbyTables:

    I have check my code last night and find how to expand two more related collections query.But I am not sure which syntax is right:

    1.

    myapp.activeDataWorkspace.ApplicationData.Researches
            .expand('ResearchAttachments,Researchers')
            .execute()
            .then(organizeByYear(promiseItem));

    2.

    myapp.activeDataWorkspace.ApplicationData.Researches
            .expand('ResearchAttachments','Researchers')
            .execute()
            .then(organizeByYear(promiseItem));


    what's more I find my promiseItem is always empty. Here is the watch data in chrome console:

    Object {totalCount: undefined, results: Array[0]}

    ->results: Array[0]

    ->length: 0

    I have add a query named ResearchQuery to get special researches.

    and I am not sure which syntax is right since both of them do not work correctly.

    First

    myapp.activeDataWorkspace.ApplicationData.ResearchQuery() .expand('ResearchAttachment','Researchers') .execute() .then( function (promiseItem) { if (promiseItem) {

    OrganizeByYear(screen.ResearchmentQuery); } });

    second

    myapp.activeDataWorkspace.ApplicationData.ResearchSet

    .expand('ResearchAttachmentCollection','EmployeeNumberCollection') .execute() .then( function (promiseItem) { if (promiseItem) { OrganizeByYear(screen.ResearchmentQuery); } });

    Thanks a lot!!!

    Saturday, November 8, 2014 2:46 AM
  • Hi,

    You should choose this

    myapp.activeDataWorkspace.ApplicationData.Researches
            .expand('ResearchAttachments','Researchers')
            .execute()
            .then(organizeByYear(promiseItem));

    • Proposed as answer by ChrisCookDev Monday, January 26, 2015 4:09 PM
    Monday, November 17, 2014 9:04 AM
  • thx a lot!!!
    Friday, December 19, 2014 1:12 PM
  • my pleasure
    Monday, December 22, 2014 1:22 AM
  • I'm not sure if this has changed but what worked for was 2 expands.

    myapp.activeDataWorkspace.AppDriveData.GetCurrentUser().expand('Employee4').expand('Employee3').execute().then(
                    function (result) {screen.TravelApplication.setHead_of_Division(result.results[0].Employee3);
                    screen.TravelApplication.setHead_of_Department(result.results[0].Employee4);
    
    )};

    Tuesday, December 22, 2015 4:01 PM