1

I am working an an old project which has to do with invoices.

My problem is that the previous programmer put some values in the wrong column.

To be more specific he put the total amount in column 'credit' instead of column 'charge'.

I want to fix those values andmove them to the correct column using linq but I don't know how to do it. I've searched on the internet but I couldn't find something similar.

I am using this code to get the invoices for the customer

foreach (Customer customer in CustomerList)
{
    foreach (KartelesPelaton p in customer.KartelesPelaton.OrderBy(p => p.Imerominia))
    {
        if (p.IsInvoice)
        {

            if (p.Credit.HasValue)
            {
                //Change this record data from p.Credit to p.Charge
            }
        }
    }
}
kara
  • 3,205
  • 4
  • 20
  • 34
rippergr
  • 182
  • 2
  • 20

2 Answers2

3

Is the following code what you need?

foreach (Customer customer in CustomerList)
{ 
    foreach (KartelesPelaton p in customer.KartelesPelaton.OrderBy(p => p.Imerominia))
    {
        if (p.IsInvoice)
        {
            if (p.Credit.HasValue)
            {
                p.Charge = p.Credit;
                p.Credit = null;
            }
        }
    }
}
kara
  • 3,205
  • 4
  • 20
  • 34
Amir Molaei
  • 3,700
  • 1
  • 17
  • 20
  • Great.Thank you. That's exactly what I wanted. I didn't believe it was that easy. In fact I made that before but I forgot to make the 'credit' column null so it gave me the same result so I thought I made it wrong and I had to save the changes. – rippergr Apr 10 '19 at 11:34
0

As mentioned in the comments Linq is for querying and not for looping.

If you want a "cool" Foreach you could do it with Parallel.Foreach:

Parallel.ForEach(CustomerList.SelectMany(c => c.KartelesPelaton), k => { if (k.IsInvoice) k.Charge = k.Credit; k.Credit = 0; });

Full example

public class Customer
{
    public int Id { get; set; }
    public List<KartelesPelaton> KartelesPelaton { get; set; }

    public override string ToString() => "Id " + this.Id + ":" + String.Join(", ", this.KartelesPelaton.Select(s => s));
}

public class KartelesPelaton
{
    public bool IsInvoice { get; set; }
    public int Credit { get; set; }
    public int Charge { get; set; }

    public override string ToString() => "Is " + (this.IsInvoice ? "" : "NOT ") + "Invoice! " + Credit + " - " + Charge;
}

public static void Main(string[] args)
{
    // Some example-data..
    List<Customer> CustomerList = new List<Customer>()
    {
        new Customer() { Id = 1, KartelesPelaton = new List<KartelesPelaton>() { new KartelesPelaton() { IsInvoice = false, Credit = 1 } } },
        new Customer() { Id = 2, KartelesPelaton = new List<KartelesPelaton>() { new KartelesPelaton() { IsInvoice = true, Credit = 2 }, new KartelesPelaton() { Credit = 22 } } },
        new Customer() { Id = 3, KartelesPelaton = new List<KartelesPelaton>() { new KartelesPelaton() { IsInvoice = true, Credit = 3 } } },
    };

    // Let's see what we got..
    Console.WriteLine("Before:");
    foreach (Customer c in CustomerList)
    {
        Console.WriteLine(c);
    }

    // Select items to modify (customers seem not to be important here..)
    var itemsToModify = CustomerList.SelectMany(c => c.KartelesPelaton);
    // Process the items
    Parallel.ForEach(itemsToModify, k => { if (k.IsInvoice) k.Charge = k.Credit; k.Credit = 0; });

    Console.WriteLine("After:");
    foreach (Customer c in CustomerList)
    {
        Console.WriteLine(c);
    }
}
kara
  • 3,205
  • 4
  • 20
  • 34
  • What difference would it make to use the Parallel.Foreach instead of foreach – rippergr Apr 10 '19 at 11:40
  • You process your jobs parallel in multiple threads. But it's only needed if you got very much work: https://stackoverflow.com/questions/6036120/parallel-foreach-slower-than-foreach – kara Apr 10 '19 at 11:48