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.
Thanks everyone for your help. I made this work by splitting it into 2 different linq queries below.
The first query gets the promotion data in a var
The second query uses the promotions var to simplify the query and get the resuls
This approach works perfectly and I get all the items whether they have promotions or not. I am still thinking whether doing the same was possible in a single query 🙂
For now marking this as an answer, if anyone comes up with some other better way will mark that.