Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

is this possible to get index of foreach loop in c#?

i want to update my db table with foreach loop and what i need is to get index of foreach loop to get right index of declared array to sum with count of my db.

byte[] OldSelectedProductsCount = ThisFactor.BuffetServices.Where(q => q.FactorID == ThisFactor.FactorID).Select(q => q.Count).ToArray();

giveBackBuffets.ForEach(t => _Buffet.Update(new BuffetViewModel()
{
    BuffetID = t.BuffetID,
    Name = t.Name,
    Price = t.Price,
    Count = t.Count + OldSelectedProductsCount[idexofforeachloop],
 }));

>Solution :

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

You can try this instead of the foreach loop

    byte[] oldSelectedProductsCount = ThisFactor.BuffetServices.Where(q => q.FactorID == ThisFactor.FactorID).Select(q => q.Count).ToArray();

    for (int i = 0; i < giveBackBuffets.Count; i++)
    {

        _Buffet.Update(new BuffetViewModel()
        {
            BuffetID = giveBackBuffets[i].BuffetID,
            Name = giveBackBuffets[i].Name,
            Price = giveBackBuffets[i].Price,
            Count = giveBackBuffets[i].Count + oldSelectedProductsCount[i],
        });
    }
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading