Copied to clipboard

Flag this post as spam?

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


  • Jeroen Breuer 4908 posts 12265 karma points MVP 5x admin c-trib
    Oct 18, 2010 @ 17:46
    Jeroen Breuer
    0

    Linq to Umbraco get single object

    Hello,

    What is the best way to get a single object in Linq to Umbraco?

    Currently this is what I'm doing:

    //Get the project node.
    Project project =
        (
            from projectNode in DigibizDataContext.Projects
            where projectNode.Id == Convert.ToInt32(projectId)
            select projectNode
        ).Single();

    In usercontrols I would like to use the Linq to Umbraco with the current node (yes I've read this). Is there a better way than this:

    //Get the current node.
    Content content =
        (
            from content in DigibizDataContext.Contents
    where content.Id == currentNodeId
    select content
    ).Single();

    Jeroen

  • Aaron Powell 1708 posts 3046 karma points c-trib
    Oct 19, 2010 @ 00:10
    Aaron Powell
    1

    UmbracoContext.Current.PageId returns an instance of Nullable<int> which will contain the current page ID, or null (if it's being used in the backoffice).

    That's the preferred way to get the current pageId, but no there isn't a way to get the "current page" from LINQ to Umbraco, it's not meant to work like that.

  • Jeroen Breuer 4908 posts 12265 karma points MVP 5x admin c-trib
    Oct 19, 2010 @ 10:33
    Jeroen Breuer
    0

    Thanks slace!

    Is there a big difference in performance between Linq to Umbraco and the NodeFactory?

    For example:

    Getting properties with the NodeFactory:

    Node currentNode = Node.GetCurrent();
    string s1 = currentNode.GetProperty("propertyName1").Value;
    string s2 = currentNode.GetProperty("propertyName2").Value;

    Getting the properties with Linq to Umbraco:

    Home home =
        (
            from homeNode in DigibizDataContext.Homes
            where homeNode.Id == UmbracoContext.Current.PageId.Value
            select homeNode
        ).Single();
    
    string s1 = home.PropertyName1;
    string s2 = home.PropertyName2;

    Can I just use both or do you recommend anything?

    Also I've build this generic method:

    public TDocType GetCurrentPage<TDocType>()
        where TDocType : DocTypeBase, new()
    {
        TDocType docType =
                (
                    from docTypeNode in DigibizDataContext.DataProvider.LoadTree<TDocType>()
                    where docTypeNode.Id == UmbracoContext.Current.PageId.Value
                    select docTypeNode
                ).Single();
    
        return docType;
    }

    Now I can get the current values like this:

    Home home = GetCurrentPage<Home>();
    
    string s1 = home.PropertyName1;
    string s2 = home.PropertyName2;

    This works but I don't know if things get slower because of this.

    Jeroen

  • Aaron Powell 1708 posts 3046 karma points c-trib
    Oct 19, 2010 @ 11:23
    Aaron Powell
    1

    Right tool for the job is my moto.

    If you want the current page to access properties you should use NodeFactory IMO (well actually I'd say go with <umbraco:Item unless you need to do anything with the property value).

    L2U is really good if you want to work with site-wide data or working with hierarchical data.

  • Jeroen Breuer 4908 posts 12265 karma points MVP 5x admin c-trib
    Oct 19, 2010 @ 12:58
    Jeroen Breuer
    0

    Hi Slace,

    You're right about the right tool for the right job. It's just that in L2U you can get all the properties strongly-typed and in the NodeFactory you need to use GetProperty which I don't like. If I still want to use L2U on the current page is there a difference in performance?

    Jeroen

     

  • Aaron Powell 1708 posts 3046 karma points c-trib
    Oct 20, 2010 @ 00:05
    Aaron Powell
    0

    I think if you do LoadTree that way you may skip the caching, I'd have to look into the code to check though.

  • Jeroen Breuer 4908 posts 12265 karma points MVP 5x admin c-trib
    Oct 20, 2010 @ 09:30
    Jeroen Breuer
    0

    Ok so LoadTree might be slower, but if I use from homeNode in DigibizDataContext.Homes instead of from docTypeNode in DigibizDataContext.DataProvider.LoadTree<TDocType>() there shouldn't be any performance problems?

    Jeroen

  • Aaron Powell 1708 posts 3046 karma points c-trib
    Oct 20, 2010 @ 11:16
    Aaron Powell
    1

    Looking into the source LoadTree does use the cache so you'll not loose performance.

  • Jeroen Breuer 4908 posts 12265 karma points MVP 5x admin c-trib
    Jan 05, 2011 @ 18:19
    Jeroen Breuer
    0

    I've been using my method to get the current page for a while now and I've run into this bug.

    Scenario:

    1 I update my documenttype
    2 I updated my Linq2Umbraco code
    3 I call my GetCurrentPage

    This worked before updating the documenttype, but now I get the following exception:

    Input string was not in a correct format. 
    Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code. 
    
    Exception Details: System.FormatException: Input string was not in a correct format.
    
    Source Error: 
    
    
    Line 77:             where TDocType : DocTypeBase, new()
    Line 78:         {
    Line 79:             TDocType docType =
    Line 80:                     (
    Line 81:                         from docTypeNode in LopitalDataContext.DataProvider.LoadTree<TDocType>()
    
    
    Source File: C:\SVN\lopitalUmbraco\Lopital.Extension\Lopital.BLL\Default\DefaultMethods.cs    Line: 79 
    
    Stack Trace: 
    
    
    [FormatException: Input string was not in a correct format.]
       System.Number.StringToNumber(String str, NumberStyles options, NumberBuffer& number, NumberFormatInfo info, Boolean parseDecimal) +9594283
       System.Number.ParseInt32(String s, NumberStyles style, NumberFormatInfo info) +119
       System.String.System.IConvertible.ToInt32(IFormatProvider provider) +46
       System.Convert.ChangeType(Object value, Type conversionType, IFormatProvider provider) +385
       System.Convert.ChangeType(Object value, Type conversionType) +32
       umbraco.Linq.Core.Node.NodeDataProvider.LoadFromXml(XElement xml, T node) +2538
       umbraco.Linq.Core.Node.NodeTree`1.GetEnumerator() +670
       System.Linq.WhereEnumerableIterator`1.MoveNext() +63
       System.Linq.Enumerable.Single(IEnumerable`1 source) +196
       Lopital.BLL.Default.DefaultMethods.GetCurrentPage() in C:\SVN\lopitalUmbraco\Lopital.Extension\Lopital.BLL\Default\DefaultMethods.cs:79
       Lopital.WebApplication.UserControls.MaintenanceContract.Page_Load(Object sender, EventArgs e) in C:\SVN\lopitalUmbraco\Lopital.Extension\Lopital.WebApplication\UserControls\MaintenanceContract.ascx.cs:43
       System.Web.Util.CalliHelper.EventArgFunctionCaller(IntPtr fp, Object o, Object t, EventArgs e) +14
       System.Web.Util.CalliEventHandlerDelegateProxy.Callback(Object sender, EventArgs e) +35
       System.Web.UI.Control.OnLoad(EventArgs e) +91
       System.Web.UI.Control.LoadRecursive() +74
       System.Web.UI.Control.LoadRecursive() +146
       System.Web.UI.Control.LoadRecursive() +146
       System.Web.UI.Control.LoadRecursive() +146
       System.Web.UI.Control.LoadRecursive() +146
       System.Web.UI.Control.LoadRecursive() +146
       System.Web.UI.Control.LoadRecursive() +146
       System.Web.UI.Control.LoadRecursive() +146
       System.Web.UI.Control.LoadRecursive() +146
       System.Web.UI.Page.ProcessRequestMain(Boolean includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint) +2207
    
    
    
    

    Any idea why this is happening?

    Jeroen

  • Aaron Powell 1708 posts 3046 karma points c-trib
    Jan 05, 2011 @ 22:46
    Aaron Powell
    0

    I'd assume you've added a numerical-based property which isn't in the XML for the page you've loaded up

  • Jeroen Breuer 4908 posts 12265 karma points MVP 5x admin c-trib
    Jan 05, 2011 @ 23:20
    Jeroen Breuer
    0

    That's what I also thought in the beginning, but I only added a text field and republished the page so everything should work....

  • Aaron Powell 1708 posts 3046 karma points c-trib
    Jan 06, 2011 @ 02:17
    Aaron Powell
    0

    Well it's failing to parse a numeric field, what ever is in the XML is not a valid number, that's all I can really tell you

  • Jeroen Breuer 4908 posts 12265 karma points MVP 5x admin c-trib
    Jan 06, 2011 @ 09:18
    Jeroen Breuer
    0

    I got it fixed by unpublishing the page and then publish it again. Somehow after changing the documenttype just publishing the page wasn't enough. Not sure if this is a bug or something. At least it works again :).

    Jeroen

  • Jeroen Breuer 4908 posts 12265 karma points MVP 5x admin c-trib
    Jan 06, 2011 @ 11:16
    Jeroen Breuer
    0

    Ok the problem is back and this time I didn't even change any documenttypes. I'm not sure what the problem is. It almost looks like its random or something.I think it's related to when the umbraco.config is updated (even if another node is published), but I'm not sure. For now I'll stop using the method until I found a solution for this.

    Another question related to this topic. I wanted to use the GetCurrentPage method because I can get all the properties strongly-typed. I thought I read somewhere that in JUNO you can get the properties from a node dynamically. If that's true I don't need this GetCurrentPage method anymore.

    Jeroen

  • Jeroen Breuer 4908 posts 12265 karma points MVP 5x admin c-trib
    Jan 06, 2011 @ 13:03
    Jeroen Breuer
    0

    Now the exception also happens with other LinqToUmbraco methods so the problem is not related the my GetCurrentPage method. Here is the exception again:

    Input string was not in a correct format. 
    Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code. 
    
    Exception Details: System.FormatException: Input string was not in a correct format.
    
    Source Error: 
    
    
    Line 254:                (
    Line 255:                    from maintenanceContractItem in LopitalDataContext.MaintenanceContractItems
    Line 256:                    where maintenanceContractItem.AncestorOrDefault().Language == language.ToString()
    Line 257:                    orderby maintenanceContractItem.SortOrder
    Line 258:                    select maintenanceContractItem
    
    
    Source File: C:\SVN\lopitalUmbraco\Lopital.Extension\Lopital.BLL\Controllers\ArticleGroupController.cs    Line: 256 
    
    Stack Trace: 
    
    
    [FormatException: Input string was not in a correct format.]
       System.Number.StringToNumber(String str, NumberStyles options, NumberBuffer& number, NumberFormatInfo info, Boolean parseDecimal) +9594283
       System.Number.ParseInt32(String s, NumberStyles style, NumberFormatInfo info) +119
       System.String.System.IConvertible.ToInt32(IFormatProvider provider) +46
       System.Convert.ChangeType(Object value, Type conversionType, IFormatProvider provider) +385
       System.Convert.ChangeType(Object value, Type conversionType) +32
       umbraco.Linq.Core.Node.NodeDataProvider.LoadFromXml(XElement xml, T node) +2538
       umbraco.Linq.Core.Node.d__6.MoveNext() +468
       System.Linq.WhereEnumerableIterator`1.MoveNext() +87
       System.Linq.d__b1`1.MoveNext() +92
       System.Linq.Enumerable.FirstOrDefault(IEnumerable`1 source, Func`2 predicate) +99
       umbraco.Linq.Core.DocTypeBase.AncestorOrDefault(Func`2 func) +382
       umbraco.Linq.Core.DocTypeBase.AncestorOrDefault() +176
       Lopital.BLL.Controllers.<>c__DisplayClass1f.b__1c(MaintenanceContractItem maintenanceContractItem) in C:\SVN\lopitalUmbraco\Lopital.Extension\Lopital.BLL\Controllers\ArticleGroupController.cs:256
       System.Linq.WhereEnumerableIterator`1.MoveNext() +141
       System.Linq.Buffer`1..ctor(IEnumerable`1 source) +217
       System.Linq.d__0.MoveNext() +96
       System.Collections.Generic.List`1..ctor(IEnumerable`1 collection) +327
       System.Linq.Enumerable.ToList(IEnumerable`1 source) +58
       Lopital.BLL.Controllers.ArticleGroupController.GetMaintenanceContractItems(Language language) in C:\SVN\lopitalUmbraco\Lopital.Extension\Lopital.BLL\Controllers\ArticleGroupController.cs:253
       Lopital.Presentation.Layer.ControllerLayer.GetMaintenanceContractItems(Language language) in C:\SVN\lopitalUmbraco\Lopital.Extension\Lopital.Presentation\Layer\ControllerLayer.cs:327
       Lopital.WebApplication.UserControls.MaintenanceContract.Page_Load(Object sender, EventArgs e) in C:\SVN\lopitalUmbraco\Lopital.Extension\Lopital.WebApplication\UserControls\MaintenanceContract.ascx.cs:46
       System.Web.Util.CalliHelper.EventArgFunctionCaller(IntPtr fp, Object o, Object t, EventArgs e) +14
       System.Web.Util.CalliEventHandlerDelegateProxy.Callback(Object sender, EventArgs e) +35
       System.Web.UI.Control.OnLoad(EventArgs e) +91
       System.Web.UI.Control.LoadRecursive() +74
       System.Web.UI.Control.LoadRecursive() +146
       System.Web.UI.Control.LoadRecursive() +146
       System.Web.UI.Control.LoadRecursive() +146
       System.Web.UI.Control.LoadRecursive() +146
       System.Web.UI.Control.LoadRecursive() +146
       System.Web.UI.Control.LoadRecursive() +146
       System.Web.UI.Control.LoadRecursive() +146
       System.Web.UI.Control.LoadRecursive() +146
       System.Web.UI.Page.ProcessRequestMain(Boolean includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint) +2207
    
    
    
    Again it seems like an interger bug, but sometimes it works and sometimes it doens't. Can't find anything which is related to an interger. After publishing all the nodes again (not with republish entire site, but the publish context item on the top node) it now works again. Very strange.

    Jeroen
  • Aaron Powell 1708 posts 3046 karma points c-trib
    Jan 07, 2011 @ 00:13
    Aaron Powell
    1

    I've put a catch around the parsing method and then rethrown with a new exception that has more information, data trying to be parsed, node ID, node alias and type it's trying to parse.

Please Sign in or register to post replies

Write your reply to:

Draft