Copied to clipboard

Flag this post as spam?

This post will be reported to the moderators as potential spam to be looked at


  • GP 16 posts 46 karma points
    Feb 26, 2013 @ 18:47
    GP
    0

    Query Umbraco DataBase

    Hi, does anybody knows if it is possible to query the umbraco database with a select? I have a custom document type named "Product" and i'd like to query the db in order to select only products with colour=red or with quantity > 100. Is it possible?  I don't use xslt just  razor macro or user control macro with umbraco 4.11.  So far I accomplish that by using uQuery.GetDocument(9999).GetChildDocuments() and then filter the list with a foreach but that loads all product in-memory.

    Thank you

    Gian Paolo

  • Andreas Iseli 150 posts 427 karma points
    Feb 27, 2013 @ 12:34
    Andreas Iseli
    0

    Sure this is possible as lang as you know the database and table architecture. But I wouln't recommend this. A better way would be a LINQ query, but I don't know how those are mapped in behind against the database. Does anyone ones more about the LINQ possibilities against the umbraco backend?

  • Lee Kelleher 4020 posts 15802 karma points MVP 13x admin c-trib
    Feb 27, 2013 @ 12:40
    Lee Kelleher
    101

    Hi Gian,

    Assuming that your content is published, you could use `uQuery.GetNodesByXPath`?

    var nodes = uQuery.GetNodesByXPath("//*[@isDoc and colour = 'red']");

    Then once you have the nodes, you can get the Document from the Id.

    Cheers, Lee.

  • Lee Kelleher 4020 posts 15802 karma points MVP 13x admin c-trib
    Feb 27, 2013 @ 12:43
    Lee Kelleher
    2

    If XPath isn't your thing... you could use LINQ syntax:

    var nodes = uQuery.GetRootNode().GetDescendantNodes(x => x.GetProperty<string>("colour") == "red");

    Cheers, Lee.

  • Andreas Iseli 150 posts 427 karma points
    Feb 27, 2013 @ 12:44
    Andreas Iseli
    0

    Thanks lee for pointing the LINQ way out.

  • Lee Kelleher 4020 posts 15802 karma points MVP 13x admin c-trib
    Feb 27, 2013 @ 12:46
    Lee Kelleher
    0

    @Andreas - since the published nodes are using the XML cache, they are fast.  Documents on the other hand, can follow the same LINQ syntax, but they will hit the database a good few times! :-(

    Cheers, Lee.

  • Hendy Racher 863 posts 3849 karma points MVP 2x admin c-trib
    Feb 27, 2013 @ 12:48
    Hendy Racher
    1

    Lee, as fast as ever !

    FYI, the XPath way would be more efficient, as only the nodes in the result set will be constructed (+ it gets compiled and cached).

  • GP 16 posts 46 karma points
    Mar 01, 2013 @ 09:56
    GP
    0

    I've decided for and xpath solution since there will be a lot of products . Thank you all very much! 

    Gian Paolo

     

  • Lee Kelleher 4020 posts 15802 karma points MVP 13x admin c-trib
    Mar 01, 2013 @ 10:35
    Lee Kelleher
    0

    Hi Gian, great, happy to help.

    (Don't forgot to mark the post as a solution)

    Thanks, Lee.

Please Sign in or register to post replies

Write your reply to:

Draft