Craig100
08/14/2023, 2:48 PMvar orderConfirmedGuid = siteRoot.DescendantsOrSelf<GeneralPage>().Where(x => x.Name == "Order Confirmed").FirstOrDefault().GetUdi().AsGuid();
I can get the Id, so I know the Name string is correct, but not the Guid.
Any ideas?
Thanks.Ambert
08/14/2023, 2:49 PMAmbert
08/14/2023, 2:49 PMAnders Bjerner
08/14/2023, 4:18 PM.FirstOrDefault()Anders Bjerner
08/14/2023, 4:23 PM.Where followed by a call to FirstOrDefault can typically be combined - eg. such as .FirstOrDefault(x => x.Name == "Order Confirmed"). So if we look past the potential issue with iterating trough descendants, your code could look as: Guid? orderConfirmedGuid = siteRoot.DescendantsOrSelf<GeneralPage>().FirstOrDefault(x => x.Name == "Order Confirmed")?.Key;Jason
08/14/2023, 4:26 PMWhere() has to iterate everything, FirstOrDefault() returns as soon as it finds a match.Anders Bjerner
08/14/2023, 4:27 PMCraig100
08/15/2023, 8:52 AMOrderConfirmedGuid = siteRoot.DescendantsOrSelf<GeneralPage>().Where(x => x.Name == "Order Confirmed").FirstOrDefault().Key.AsGuid(); but now I have OrderConfirmedGuid = siteRoot.DescendantsOrSelf<GeneralPage>().FirstOrDefault(x => x.Name == "Order Confirmed").Key.AsGuid(); šDean Leigh
08/15/2023, 9:09 AMAnders Bjerner
08/15/2023, 9:23 AM.Key returns a GUID, so the extra .AsGuid() shouldn't be necessary (I'm not exacly sure what it does)Jason
08/15/2023, 11:39 AMcsharp
public static Guid AsGuid(this object value) => value is Guid guid ? guid : Guid.Empty;
š¬