10

Left Join Linq and use the variable for other joins

 2 years ago
source link: https://www.codesd.com/item/left-join-linq-and-use-the-variable-for-other-joins.html
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.

Left Join Linq and use the variable for other joins

advertisements

I am stuck with the error "value cannot be null. parameter name row" when I use Left join in Linq for DataSet.

Following is the data and the linq query.

  • DataRowCollection - items (Columns - Item_id, SKU, Quantity)
  • DataRowCollection - promotions (Columns - Item_id, Promotion_Id)
  • DataRowCollection - components (Columns - Component_id, Promotion_id)
  • DataRowCollection - amounts (Columns - Component_id, Amount_Text, currency)
        var q =
            from Item in items
            join promotion in promotions
                on Item.Field<int>("Item_id") equals promotion.Field<int?>("Item_id") into promo
            from promotion in promo.DefaultIfEmpty()
            join disccomponent in components
                on promotion.Field<int>("Promotion_Id") equals disccomponent.Field<int?>("Promotion_Id")
            join discamounts in amounts
                on disccomponent.Field<int>("Component_id") equals discamounts.Field<int?>("Component_id")
            where disccomponent.Field<string>("Type") == "Principal"
            select new
            {
                SKU = Item.Field<string>("SKU"),
                Quantity = Item.Field<string>("Quantity"),
                DiscountAmount = discamounts.Field<string>("Amount_Text"),
                DiscountCurrency = discamounts.Field<string>("currency")
            };

I need to get the following:

  • SKU, Qty and Discount details for items which have discount
  • SKU, Qty for items which do not have any discount

The code worked without left outer join when all items had discount, but if any item does not have any discount it skips that item and hence I had to use the left outer join.

Any help is highly appreciated. Please let me know if any clarification is needed.

Thanks in advance.


I think you are trying to access properties on a null discamounts. Perhaps your select new should look something like this...

select new
{
    SKU = Item.Field<string>("SKU"),
    Quantity = Item.Field<string>("Quantity"),
    DiscountAmount = discamounts == null ? "" : discamounts.Field<string>("Amount_Text"),
    DiscountCurrency = discamounts == null ? "" : discamounts.Field<string>("currency")
};


About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK