Simplify C# code by Removing lambda expressions

BigH61 581 Reputation points
2022-03-19T16:01:20.453+00:00

I am relatively new to coding and find my best method of learning is examining existing code and were necessary deconstructing expressions into once I can more easily follow.
One of the areas I really struggle with is lambda and I appreciate it is something I will need to get my head round sooner rather than later. However I am struggling to deconstruct the following into simpler lambda free code.

private ICommand _reloadSrc;
public ICommand ReloadSourcesCommand
            {
                get
                {
                    return _reloadSrc ?? (_reloadSrc = new RelayCommand(() =>
                    {
                        DataSources.Clear();
                        foreach (var s in _session.Select(s => new DataSourceVM { DS = s }))
                        {
                            DataSources.Add(s);
                        }
                    }, () =>
                    {
                        return _session.State > 2;
                    }));
                }
            }

What I have so far is

DataSourceVM dataSourceVM;

        public ICommand ReloadSourcesCommand
        {
            get
            {
                return _reloadSrc ?? (_reloadSrc = new RelayCommand(() =>
                {
                    DataSources.Clear();
                    foreach (var s in _session)
                    {
                        dataSourceVM = new DataSourceVM
                        {
                            DS = s,
                        };
                        DataSources.Add(dataSourceVM);
                    }
                }, () =>
                {
                    return _session.State > 2;
                }));
            }
        }

I assume what I have done is correct as the programme still works and it appears to fit in with what I understand so far. But could anyone help converting the remainder of the code. The programme I am looking at has a number of similar expressions and I’m hoping I will be able to do the same to these once I can compare the code to some I can more easily understand.
Thank you in advance

Windows Presentation Foundation
Windows Presentation Foundation
A part of the .NET Framework that provides a unified programming model for building line-of-business desktop applications on Windows.
2,710 questions
0 comments No comments
{count} votes

Accepted answer
  1. Viorel 114.7K Reputation points
    2022-03-19T19:44:23.42+00:00

    Check if this modification is quite correct:

    public ICommand ReloadSourcesCommand
    {
       get
       {
          if( _reloadSrc == null) 
          {
             _reloadSrc = new RelayCommand( ( ) =>
                {
                   DataSources.Clear();
                   foreach( var s in _session) DataSources.Add( new DataSourceVM { DS = s } );
                }, 
                ( ) => _session.State > 2 );
          }
    
          return _reloadSrc;
       }
    }
    

0 additional answers

Sort by: Most helpful