0

I have this function:

public void RemoveCustomAssignmentFromPublishers(int iCustomAssignment)
{
    try
    {
        // We need to examine each Publisher in the dictionary
        foreach (KeyValuePair<string, Publisher> entry in _PublisherData.PublisherDictionary)
        {
            // We must remove all default exclusions where the stated custom assignment index is used
            entry.Value.Assignments.CustomAssignments.RemoveAll(x => x.Index == iCustomAssignment);
        }
    }
    catch (Exception ex)
    {
        SimpleLog.Log(ex);
    }
}

It iterates a map and then uses LINQ to remove all items as indicated.

Can we use LINQ instead of the foreach loop?

Andrew Truckle
  • 17,769
  • 16
  • 66
  • 164
  • 11
    why do you want that? Your code is absolutely okay, no reason to encrypt it using some fancy one-liner-LINQ. Anyway the Q in LINQ stands for "querying". So you cannot update an existing collection using linq, you can only query one and store its result in something different. Having said this you won't gain much, in particular you need to be able to **set** the `Assignemtns`. – MakePeaceGreatAgain Mar 02 '22 at 08:34
  • 2
    @HimBromBeere Alright - thanks for the brute force honesty. :) I will leave it as it is! – Andrew Truckle Mar 02 '22 at 08:37
  • 2
    Brute-force-honesty, nice phrase. Actually you **could*Ü do it, however I won't recommend it unless you have some specific problem with your code. As it is it is easy to understand and it gets its job done. – MakePeaceGreatAgain Mar 02 '22 at 08:46
  • 2
    Since you only access `entry.Value`, the one thing I would do for clarity is `foreach (var publisher in _PublisherData.PublisherDictionary.Values) { publisher.Assignments.... }` – canton7 Mar 02 '22 at 08:49
  • 2
    Actually you can hide a forEach inside a .Select plus a fuction, but no need for it in my opinion – J.Salas Mar 02 '22 at 08:56
  • A combination of select and DeleteOnSubmit could be replaced with foreach loop. But as I'm not sure about your list/table items. I can't suggest a code as answer. – redParrot Mar 04 '22 at 04:56

0 Answers0