locked
lightswitch html use the 'any' query method RRS feed

  • Question

  • Hi

    Can someone please tell me how i can use the "any" query method for entity collections in lightswitch html?

    Thanks



    • Edited by M. Lerner Tuesday, February 17, 2015 4:39 PM
    Tuesday, February 17, 2015 4:38 PM

Answers

  • The only one I am aware of is the 'any' for arrays which determines if a sequence contains a condition (just like LINQ Any in c#). Used with msls.iterate.

    if (msls.iterate(entities).any(function () {
        return this.details.hasEdits;
    }))
    { ...
    

    Other examples are in msls-{version}.js script.

    Dave


    Dave Baker | AIDE for LightSwitch | Xpert360 blog | twitter : @xpert360 | Xpert360 website | Opinions are my own. For better forums, remember to mark posts as helpful/answer.

    • Proposed as answer by ADefwebserver Wednesday, February 18, 2015 7:03 PM
    • Marked as answer by Angie Xu Tuesday, February 24, 2015 11:24 AM
    Wednesday, February 18, 2015 4:27 PM
  • Hi, it is similar to the previous example msls.iterate, it applies to msls.Sequence.

    I could not find a single example anywhere in Bing or Google for this, I had to look inside the msls codes to find how to use it. Here is an example:

    var seq = new msls.Sequence.Array(screen.Employees.data);
    if (seq.any(function (item) {
        return item.Country == "USA";
    })) {
        msls.showMessageBox("Contains USA citizens");
    } else {
        msls.showMessageBox("No USA citizens");
    }
    

    There are methods: all, any, each, first, sum, where

    JavaScript arrays have 'some', 'map', 'pop', 'push' so there is some overlap, perhaps these names are more obvious meaning for some devs. The function 'map' is obvious, is it not???

    Enjoy!

    Dave


    Dave Baker | AIDE for LightSwitch | Xpert360 blog | twitter : @xpert360 | Xpert360 website | Opinions are my own. For better forums, remember to mark posts as helpful/answer.

    • Proposed as answer by Xpert360 Friday, February 20, 2015 10:59 AM
    • Marked as answer by Angie Xu Tuesday, February 24, 2015 11:24 AM
    Friday, February 20, 2015 10:58 AM

All replies

  • Any is a linq method. If (screen.Products.any(p => p.stock == 0) ... http://blogs.msdn.com/b/lightswitch/archive/2011/10/03/advanced-lightswitch-writing-queries-in-lightswitch-code.aspx So you should be looking for linq docs and tutorials. Dave

    Dave Baker | AIDE for LightSwitch | Xpert360 blog | twitter : @xpert360 | Xpert360 website | Opinions are my own. For better forums, remember to mark posts as helpful/answer.

    Tuesday, February 17, 2015 6:12 PM
  • Any is a linq method. If (screen.Products.any(p => p.stock == 0) ... http://blogs.msdn.com/b/lightswitch/archive/2011/10/03/advanced-lightswitch-writing-queries-in-lightswitch-code.aspx So you should be looking for linq docs and tutorials. Dave

    Dave Baker | AIDE for LightSwitch | Xpert360 blog | twitter : @xpert360 | Xpert360 website | Opinions are my own. For better forums, remember to mark posts as helpful/answer.

    Sorry but i'm talking about the any method in java script that's available with the lightswitch api.
    Wednesday, February 18, 2015 1:24 AM
  • I'm not aware of it, do you have an example?  Where it is available?
    Wednesday, February 18, 2015 3:06 AM
  • The only one I am aware of is the 'any' for arrays which determines if a sequence contains a condition (just like LINQ Any in c#). Used with msls.iterate.

    if (msls.iterate(entities).any(function () {
        return this.details.hasEdits;
    }))
    { ...
    

    Other examples are in msls-{version}.js script.

    Dave


    Dave Baker | AIDE for LightSwitch | Xpert360 blog | twitter : @xpert360 | Xpert360 website | Opinions are my own. For better forums, remember to mark posts as helpful/answer.

    • Proposed as answer by ADefwebserver Wednesday, February 18, 2015 7:03 PM
    • Marked as answer by Angie Xu Tuesday, February 24, 2015 11:24 AM
    Wednesday, February 18, 2015 4:27 PM
  • Hi Xpert360

    It's for a type of EntityCollection this is the code from the msls-2.5.2.vsdoc.js line 2220

            any: function any(predicate) {
                /// <summary>
                /// Determines whether this sequence contains any
                /// items that optionally satisfy a condition.
                /// </summary>
                /// <param name="predicate" type="Function"  optional="true">
                /// A function to test each item for a condition.
                /// <br/>Signature: Boolean item.predicate(item)
                /// </param>
                /// <returns type="Boolean">
                /// True if this sequence contains any items that
                /// satisfy the condition, if any; otherwise, false.
                /// </returns>
                var result = false;
                this.each(function (item) {
                    if (!predicate || predicate.call(item, item)) {
                        result = true;
                        return false;
                    }
                    return true;
                });
                return result;
            },

    Check it out and let me know if you know how to use it, my problem is how to use the call back function.

    Thanks


    • Edited by M. Lerner Wednesday, February 18, 2015 10:13 PM
    Wednesday, February 18, 2015 10:12 PM
  • Hi, it is similar to the previous example msls.iterate, it applies to msls.Sequence.

    I could not find a single example anywhere in Bing or Google for this, I had to look inside the msls codes to find how to use it. Here is an example:

    var seq = new msls.Sequence.Array(screen.Employees.data);
    if (seq.any(function (item) {
        return item.Country == "USA";
    })) {
        msls.showMessageBox("Contains USA citizens");
    } else {
        msls.showMessageBox("No USA citizens");
    }
    

    There are methods: all, any, each, first, sum, where

    JavaScript arrays have 'some', 'map', 'pop', 'push' so there is some overlap, perhaps these names are more obvious meaning for some devs. The function 'map' is obvious, is it not???

    Enjoy!

    Dave


    Dave Baker | AIDE for LightSwitch | Xpert360 blog | twitter : @xpert360 | Xpert360 website | Opinions are my own. For better forums, remember to mark posts as helpful/answer.

    • Proposed as answer by Xpert360 Friday, February 20, 2015 10:59 AM
    • Marked as answer by Angie Xu Tuesday, February 24, 2015 11:24 AM
    Friday, February 20, 2015 10:58 AM
  • Ok Thanks
    Thursday, February 26, 2015 2:23 AM
  • If I recall correctly, msls.iterate uses msls.sequence internally so the methods ( any all first where sum each ) are available to msls.iterate as well. Here is an example of msls.iterate().where(). http://joshuabooker.com/Blog/Post/8/Monkey-Patched-LightSwitch-Screen-Collection-Queries
    Thursday, February 26, 2015 2:49 AM