2

I have 3 levels of lists one inside other as below

ProductResults
    ErrorNo
    ErrorString
    Products
        ProductNo
        ProductName
        SVNID
        ProductOrders
            OrderNo
            DateOfOrder
            UDEID

I want to check the value of UDEID in the lowest list. If the value is 77 then I want to update the SVNID in the Products list above. How can I achieve this? I have tried below

ProductResultsList.Where(x => x.Products.Any(y => y.ProductOrders.Any(z => z.UDEID == 77))).ToList().
    ForEach(u =>
    {
        u.Products.ForEach(v => v.SVNID = 90);
    });

My data

 ProductResultsList.ErrorNo = 0
 ProductResultsList.ErrorString = ""
 ProductResultsList.Products[0].ProductNo = "XY6789U"
 ProductResultsList.Products[0].ProductName = "OrangeJuice"
 ProductResultsList.Products[0].SVNID = 100
 ProductResultsList.Products[0].ProductOrders[0].OrderNo = 201
 ProductResultsList.Products[0].ProductOrders[0].DateOfOrder = 28/09
 ProductResultsList.Products[0].ProductOrders[0].UDEID = 77
 ProductResultsList.Products[1].ProductNo = "XY4569U"
 ProductResultsList.Products[1].ProductName = "AppleJuice"
 ProductResultsList.Products[1].SVNID = 100
 ProductResultsList.Products[1].ProductOrders[0].OrderNo = 202
 ProductResultsList.Products[1].ProductOrders[0].DateOfOrder = 28/09
 ProductResultsList.Products[1].ProductOrders[0].UDEID = 88

The problem with the above is that it updates all Products.SVNID to 90. I just want to update Products[0] and not Products[1]. I do understand why its happening because my ForEach is ProductResults but then I don't know how to do what I want.

Please help

3
  • 4
    Try u.Products[0].SVNID = 90; instead of u.Products.ForEach(v => v.SVNID = 90); Commented Sep 28, 2018 at 8:27
  • This will update only first item in Products. I want to do it in the entire list Commented Sep 28, 2018 at 8:35
  • Please note that the Q in Linq means "query". You are trying to update. ForEach is not a part of Linq, so you are using Linq (for example the Where and Any calls, but your solution might not involve Linq.). Commented Sep 28, 2018 at 8:55

1 Answer 1

4

Your problem lays in the first, outer "Any":

ProductResultsList.Where(x => x.Products.Any( 'condition'))

Since the 'condition' is met, because there is 'Any' product, that has ProductOrder with UEID = 77, you get ALL ProductResult retured to ForEach

You should 'insert' loop deeper, sth like (not tested):

ProductResultsList.ForEach(productResult => {
    productResult.Product.Where(product => 
        product.Any('yourCondition')).ToList()
    .ForEach('your action')

Side note: why not to extract it to regular method? As you see, It is hard to track what is going on from the very beginning. With named method it would be easier.

Sign up to request clarification or add additional context in comments.

3 Comments

Thanks this works - although I had to add ToList() before ForEach but a perfect linq
@user2837961 feel free to edit my answer to put the proper, tested solution. Mine was compiled in my (not_so_ideal) mind
I have edited the answer to correctly working solution

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.