Need help writing a block of code with linq

Mančius

New member
Joined
May 20, 2019
Messages
1
Programming Experience
1-3
Hey. I'm new to linQ and I was wondering how can I write the same block of code using linq.

C#:
         List<MemorableD> memorables = new List<MemorableD>();
        List<StateMD> states = new List<StateMD>();
        void Find(List<MemorableD> selected)
        {
            for (int i = 0; i < states.Count; i++)
            {
                for (int j = 0; j < memorables.Count; j++)
                {
                    if (states[i].Month == memorables[j].Month && states[i].Day == memorables[j].Day)
                    {
                        MemorableD select = new MemorableD(memorables[j].Year, memorables[j].Month, memorables[j].Day, memorables[j].Event, states[i].Event);
                        selected.Add(select);
                    }
                }
            }
        }
 
Take a look at the "Compound from clauses" example here from clause - C# Reference
That's what you could do with your two collections, the where check for equality, and the select to create the new object.
 
Last edited:
Something like this should work:

C#:
var selected = states.Join(memorables,
                           s => new { s.Month, s.Day },
                           m => new { m.Month, m.Day },
                           (s, m) => new MemorableD(m.Year, m.Month, m.Day, m.Event, s.Event));
 
Back
Top Bottom