locked
Fetch XML V LINQ RRS feed

  • Question

  • I wrote a plugin that appeared to work as it should, the only issue is when there are nulls in the returned data the code errors:

    using (GeneratedCode orgcontext = new GeneratedCode(service))
                        {
    
                            var retrievedProducts = (from p in orgcontext.ProductSet
                                                     join qd in orgcontext.QuoteDetailSet
                                                     on p.ProductId equals qd.sp_SellingProductId.Id
                                                     where qd.QuoteId.Id == entity.Id                                                 
                                                     select new
                                                     {
                                                         product_name = p.orb_ParentProductid.Name
                                                     }).Distinct();
    
    
                            var _txttowrite = string.Empty;
                            if (retrievedProducts != null)
                            {
                                // for each item returned write the output to a string.
    
                                foreach (var item in retrievedProducts)
                                {
                                    if (item.product_name != "")
                                    {
                                        if (String.IsNullOrEmpty(_txttowrite))
                                        {
                                            _txttowrite = item.product_name;
                                        }
                                        else
                                        {
                                            _txttowrite += ", " + item.product_name;
                                        }
                                    }
                                }
    
    
                            }

    Now as I have been struggling to get this working I constructed a similar query in FetchXML, which dealt with the null values and outputted the results:

    <?xml version="1.0"?>
    
    -<fetch distinct="false" mapping="logical" output-format="xml-platform" version="1.0">
    
    
    -<entity name="quotedetail">
    
    <attribute name="quotedetailid"/>
    
    
    -<link-entity name="quote" alias="ab" to="quoteid" from="quoteid">
    
    
    -<filter type="and">
    
    <condition value="QUO-02095-L7W6" operator="eq" attribute="name"/>
    
    <condition value="10" operator="eq" attribute="revisionnumber"/>
    
    </filter>
    
    </link-entity>
    
    
    -<link-entity name="product" alias="a_cf8cac3f6909e211a4d002bf0a86f1e1" to="sp_sellingproductid" from="productid">
    
    <attribute name="orb_parentproductid"/>
    
    
    -<filter type="and">
    
    <condition operator="not-null" attribute="orb_parentproductid"/>
    
    </filter>
    
    </link-entity>
    
    </entity>
    
    </fetch>

    The issue with the fetchXML is that it is tied into a single quote and revision, how can I get it to work with the current quote i.e. make it dynamic?

    The second question is I want to pass the retrieved parentproduct out to a text field how do I get access to this?  must admit I am getting myself confused with all this.

    any help appreciated.

    Matt

    Wednesday, August 13, 2014 3:00 PM

Answers

  • It seems like the only problem with your Linq query is you are attempting to assign:

    product_name = p.orb_ParentProductid.Name

    in your select.  Thus the statement chokes if p.orb_ParentProductid is null. If you change this to:

    product_name = p.orb_ParentProductid

    you can subsequently check for null in product_name and use product_name.Name if it is not null.

    • Marked as answer by Matt_Hirst_UK Thursday, August 14, 2014 8:25 AM
    Wednesday, August 13, 2014 8:44 PM

All replies

  • It seems like the only problem with your Linq query is you are attempting to assign:

    product_name = p.orb_ParentProductid.Name

    in your select.  Thus the statement chokes if p.orb_ParentProductid is null. If you change this to:

    product_name = p.orb_ParentProductid

    you can subsequently check for null in product_name and use product_name.Name if it is not null.

    • Marked as answer by Matt_Hirst_UK Thursday, August 14, 2014 8:25 AM
    Wednesday, August 13, 2014 8:44 PM
  • Hi,

    The statement does fail as you said with nulls at the line foreach (var item in retrievedProducts) with "object reference not set to an instance of an object".

    I have added the null-coalescing statement here product_name = p.orb_ParentProductid.Name ?? p.Name but it still fails with the same error.

    Have updated the code to:

     var retrievedProducts = (from p in orgcontext.ProductSet
                                                     join qd in orgcontext.QuoteDetailSet
                                                     on p.ProductId equals qd.sp_SellingProductId.Id
                                                     where qd.QuoteId.Id == entity.Id
                                                     select new
                                                     {
                                                         product_name = p.orb_ParentProductid
                                                     }).Distinct();
    
                            var _txttowrite = string.Empty;
                            if (retrievedProducts != null)
                            {
                                // for each item returned write the output to a string.
    
                                foreach (var item in retrievedProducts)
                                {
                                    if (item.product_name != null)
                                    {
                                        if (String.IsNullOrEmpty(_txttowrite))
                                        {
                                            _txttowrite = item.product_name.Name;
                                        }
                                        else
                                        {
                                            _txttowrite += ", " + item.product_name.Name;
                                        }
                                    }
                                }
    
    
                            }

    and sir you are a star it works as intended!

    regards,

    Matt

    Thursday, August 14, 2014 8:25 AM