0

I have two lists: orders and items and I need to compare them by their name AND then look if order list have bool set to false and if it satysfy the condition it copies its string message and then adds to new list and I just have no freaking idea even how to compare lists, so any help will be appreciated Function:

 private static void FailedItemsList(List<Item> failed, List<Order> orders, List<Item> items)
{
    foreach(var order in orders)
    {
        foreach(var item in items)
        {
            if(order.Equals(item))
            {
                if(order.Fulfilled == false)
                {
                    item.FailMessage = order.FailMessage;
                    failed.Add(item);
                }
            }
        }
    }
}
4
  • What do you mean by compare? What's the needed output? Commented May 25, 2016 at 7:40
  • I forgot to say that Equals compares order.Name to item.Name, so in linq i need that it would compare thei names and if it matches then check if order.Fulfilled == false and if it is then item should copy FailMessage and add that item to new list, I know its a big mess but thats the thing i was asked :( Commented May 25, 2016 at 7:43
  • Are you sure that your code works, Order is a different class than Item but you are using order.Equals(item)? Commented May 25, 2016 at 7:48
  • Yeah it works, because It was public class Order : IEquatable<Item> and had Equals method, who compared and your GetFailedItems method worked just fine, tanks Commented May 25, 2016 at 9:42

2 Answers 2

2

In general i would return this "fail-list" from the method instead of passing it to it. You could use LINQ to simplify this task:

static List<Item> GetFailedItems(List<Order> orders, List<Item> items)
{
    var failed = from order in orders
                 where !order.Fulfilled
                 join item in items on order.Name equals item.Name
                 select new { order, item};

    List<Item> failedItems = new List<Item>();
    foreach (var x in failed)
    {
        x.item.FailMessage = x.order.FailMessage;
        failedItems.Add(x.item);
    }

    return failedItems;
}

Since Item and Order are different classes i have removed the Equals check and replaced it with a join on the Name property as desired. Since LINQ should not cause side effects i haven't modifed the Item.FailMessage property in the query but in the foreach-loop. Due to LINQ's deferred execution(the query gets executed at the foreach) this is not less efficient.

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

Comments

0
var v = from x in orders
        where x.Fulfilled == false
        join item in items on order.Name equals item.Name
        select x.FailMessage;

foreach (int i in v)
    failed.Add(i);

Comments

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.