Page 1 of 1

Load a Product Template Field for a Product

Posted: Fri Jun 05, 2015 5:33 am
by rmaweb
Hello,

I am working on some site customization and I was wondering if there was a method or an nhibernate call to be able to load the value of a specific product template field for a product. As opposed to using

Code: Select all

List<ProductTemplateField> templateFields = (from tf in product.TemplateFields
                                                         where tf.InputField.Name == "xxx"
                                                         select tf).ToList();
and having to do a foreach loop to get the field I want. Is there a way to skip the List and get the value directly?

Re: Load a Product Template Field for a Product

Posted: Fri Jun 05, 2015 6:26 am
by jguengerich
I think you could do:

Code: Select all

string myValue = product.TemplateFields.Where(tf => tf.InputField.Name == "xxx").Select(f => f.InputValue).First();
You'll need "using System.Linq;" if you don't have it already. This also makes the assumption that there is a field named "xxx", so you may want to add a little defensive programming, perhaps using FirstOrDefault() instead of First(). I didn't test this code.

EDIT: using the query syntax, I guess that would be:

Code: Select all

            string theValue = (from tf in product.TemplateFields
                                                         where tf.InputField.Name == "xxx"
                                                         select tf.InputValue).First();  // see also .Single()/.SingleOrDefault()

Re: Load a Product Template Field for a Product

Posted: Fri Jun 05, 2015 7:07 am
by rmaweb
Thank you Jay,

I will give this a shot. I have no experience with nhibernate queries or Linq, so thank you for pointing me in the right direction.